Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPBGU] A09 Alexander Chvanov #1456

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 119 additions & 2 deletions src/Expr.lama
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,21 @@ fun addNames (state, names) {
fun addFunction (state, name, args, body) {
state.addName (name, Fun (args, body))
}


fun addNamesAndFuns (state, defs) {
foldl (fun (state, def) {
case def of
Var(name) -> state.addNames(name)
| Val(name) -> state.addNames(name)
| Fun(name, args, body) -> state.addFunction(name, args, body)
esac
}, state, defs)
}

fun addArgs (state, names, values) {
foldl (fun (s, [name, value]) {s.addName(name, value)}, state, zip(names, values))
}

-- Evaluates a list of expressions, properly threading a configurations.
-- Returns the final configuration and the list of values
fun evalList (c, exprs) {
Expand All @@ -102,7 +116,110 @@ fun evalList (c, exprs) {
}

fun eval (c@[s, w], expr) {
failure ("evalExpr not implemented\n")
case expr of
Assn (e1, e2) -> case eval(c, e2) of
[c1, value] -> case eval(c1, e1) of
[[s, w], ElemRef (arr, ind)] -> arr[ind] := value; [[s, w], value]
| [[s1, w1], refName] -> [[s1 <- [refName, value], w1], value]
esac
esac
| Seq (e1, e2) -> case eval(c, e1) of
[c1, _] -> eval(c1, e2)
esac
| Skip -> [c, Void]
| If (e, e1, e2) -> case eval(c, e) of
[c1, res] -> if (res != 0) then eval(c1, e1) else eval(c1, e2) fi
esac
| While (e, e1) -> case eval(c, e) of
[c1, res] -> if (res) then eval(c1, Seq(e1, While(e, e1))) else [c1, Void] fi
esac
| DoWhile (e1, e) -> eval(c, Seq(e1, While (e, e1)) )
| Var (st) -> [c, lookup(s, st)]
| Ref (st) -> [c, st]
| Const (n) -> [c, n]
| Binop (st, e1, e2) -> case eval(c, e1) of
[c1, res1] -> case eval(c1, e2) of
[c2, res2] -> [c2, evalOp(st, res1, res2)]
esac
esac
| Ignore (e) -> case eval(c, e) of
[c1, _] -> [c1, Void]
esac
| Scope (defs, expr) -> case enterScope(s) of
s1 -> case addNamesAndFuns(s1, defs) of
s2 -> case eval([s2, w], expr) of
[[s3, w1], res] -> [[leaveScope(s3), w1], res]
esac
esac
esac
| Call (name, args) -> case evalList(c, args) of
[[s1, w1], argVals] -> case lookup(s1, name) of
Fun (argNames, External) -> case evalBuiltin(name, argVals, w1) of
[result, newWorld] -> [[s1, newWorld], result]
esac
| Fun(argNames, body) -> case enterFunction(s1) of
sFun -> case addArgs(sFun, argNames, argVals) of
sFun1 -> case eval([sFun1, w1], body) of
[[sFun2, w2], res] -> [[leaveFunction(s1, getGlobal(sFun2)), w2], res]
esac
esac
esac
esac
esac
| Set (name, expr) -> case eval(c, expr) of
[[s, w], value] -> [[s <- [name, value], w], Void]
esac
| String (stringVal) -> [c, stringVal] -- Shrug
| Elem (arrExpr, indExpr) -> case eval(c, arrExpr) of
[c, Sexp(name, args)] -> case eval(c, indExpr) of
[c, ind] -> [c, args[ind]] -- Simple and stupid
esac
| [c, arr] -> case eval(c, indExpr) of
[c, ind] -> [c, arr[ind]] -- Simple and stupid
esac
esac
| ElemRef (arrExpr, indExpr) -> case eval(c, arrExpr) of
[c, Sexp(name, args)] -> case eval(c, indExpr) of
[c, ind] -> [c, ElemRef (args, ind)]
esac
| [c, arr] -> case eval(c, indExpr) of
[c, ind] -> [c, ElemRef (arr, ind)] -- -\( =/ )/-
esac
esac
| Array (exprs) -> case evalList(c, exprs) of
[c, vals] -> [c, listArray(vals)]
esac
| Sexp (tag, exprs) -> case evalList(c, exprs) of
[c, vals] -> [c, Sexp(tag, listArray(vals))]
esac
| Case (valvarName, branches) -> case s.lookup(valvarName) of
value -> choseAndEvalBranch(c, value, branches)
esac
| _ -> failure ("Part %s not implemented\n", expr.string)
esac
}

fun choseAndEvalBranch(c, value, [pattern, exp]:branches) {
if (match(pattern, value)) then eval(c, exp) else choseAndEvalBranch(c, value, branches) fi
}

fun compareLists(patts, vals) {
if (patts.size != vals.size) then
false
else foldl (fun (acc, [pat, vall]) {
if acc then match(pat, vall) else false fi
}, true, zip (patts, vals))
fi
}

fun match(pattern, value) {
case pattern of
Wildcard -> true
| Named (name, patt) -> match(patt, value)
| Const (value) -> true
| Array (pats) -> case value of Array (vals) -> compareLists(pats, arrayList(vals)) | _ -> false esac
| Sexp (ptag, pats) -> case value of Sexp (vtag, vals) -> (compare(ptag, vtag) == 0) && compareLists(pats, arrayList(vals)) | _ -> false esac
esac
}


Expand Down
92 changes: 90 additions & 2 deletions src/Parser.lama
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@ fun reifyPatternBindings (matched, brs) {
)
}

var elseBranch = memo $ eta syntax (
loc=pos kFi {fun (a) { assertVoid(a, returnST $ Skip, loc) } }
| loc=pos kElse e=scopeExpr kFi {fun (a) { e(a)} }
| kElif e=exp kThen e1=scopeExpr e2=elseBranch {fun (a) { e(Val) =>> fun (e) { e1(a) =>> fun (e1) { e2(a) => fun (e2) { If (e, e1, e2) } } } } }
);

var
primary = memo $ eta syntax (
-- array constant
Expand Down Expand Up @@ -224,9 +230,91 @@ var
None -> returnST $ Sexp (x, {})
| Some (args) -> chainST (args) => fun (args) {Sexp (x, args)}
esac, loc)}
} |
}

$(failure ("the rest of primary parsing in not implemented\n"))),
| loc=pos kSkip {fun (a) { assertVoid(a, returnST $ Skip, loc) } }
| loc=pos kIf e=exp kThen e1=scopeExpr e2=elseBranch {fun (a) {
e(Val) =>> fun (e) {
e1(a) =>> fun (e1) {
e2(a) => fun (e2) {
If (e, e1, e2)
}
}
} } }
| loc=pos kWhile e=exp kDo e1=scopeExpr kOd {fun (a) { assertVoid(a,
e(Val) =>> fun (e) {
e1(Void) => fun (e1) {
While(e, e1)
}
}, loc) } }
| loc=pos kFor e1=scopeExpr s[","] e=scopeExpr s[","] e3=exp kDo e4=exp kOd {fun (a) { assertVoid(a,
e1(Void) =>> fun (e1) {
e(Val) =>> fun (e) {
e4(Void) =>> fun (e4) {
e3(Void) => fun (e3) {
distributeScope(e1, fun (e1) {Seq (e1, While (e, Seq(e4, e3))) } )
}
}
}
}, loc)} }
| loc=pos kDo e1=scopeExpr kWhile e=exp kOd {fun (a) { assertVoid(a,
e1(Void) =>> fun (e1) {
e(Val) => fun (e) {
distributeScope(e1, fun(e1) { DoWhile(e1, e) })
}
}, loc) } }
| loc=pos e=inbr[s("("), scopeExpr, s(")")] {fun (a) { e(a) } }
| loc=pos kCase e=exp kOf brs=branches kEsac { fun (a) {
freshName =>> fun (name) {
e(Val) =>> fun(e) {
brs(a) => fun(brs) {
Scope({ Var({name}) },
Seq(Ignore(Assn(Ref(name), e)), Case (name, reifyPatternBindings(name, brs)))
)
}
}
} } }
),
pattern = memo $ eta syntax (
-- Pattern in ( )
inbr[s ("("), pattern, s (")")]
-- Wildcard
| s["_"] {fun (a) { returnST $ Wildcard }}
-- Single number
| loc=pos x=decimal {
fun (a) {assertValue (a, returnST $ Const (stringInt (x)), loc)}
}
-- Array
| loc=pos x=inbr[s("["), list0(syntax (e=pattern {e(Val)})), s("]")] {
fun (a) {assertValue (a, chainST (x) => fun (x) {Array (x)}, loc)}
}
-- Single name
| loc=pos name=lident {
fun (a) {
returnST $ Named(name, Wildcard)
}
}
-- Named pattern
| loc=pos name=lident p=(-s["@"] pattern) {
fun (a) {
p(Val) => fun (p) { Named (name, p) }
}
}
-- Sexp pattern
| loc=pos x=uident args=inbr[s ("("), list0(syntax(p=pattern { p (Val) })), s (")")]? {
fun (a) {assertValue (a,
case args of
None -> returnST $ Sexp (x, {})
| Some (args) -> chainST (args) => fun (args) {Sexp (x, args)}
esac, loc) }
}
),
branch = memo $ eta syntax (
loc=pos p=pattern s["->"] s=scopeExpr {fun (a) {p (Val) =>> fun (p) {s (a) =>> fun (s) { returnST $ [p, s] }}}}
),
branches = memo $ eta syntax (
loc=pos brs=listBy[branch, s("|")] {fun (a) { chainST(map (fun(br) {br (a)}, brs)) } }
),


basic = memo $ eta (
Expand Down
Loading
Loading