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

A07, Jegor Popow #1195

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
166 changes: 164 additions & 2 deletions src/Expr.lama
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,171 @@ fun evalList (c, exprs) {
}

fun eval (c@[s, w], expr) {
failure ("evalExpr not implemented\n")
}

fun applyAssign(c, lvalue, rvalue) {
case evalList(c, {lvalue, rvalue}) of
[c, {to, from}] -> case to of
Ref(name) -> case c of
[s, w] -> [[s <- [name, from], w], from]
esac
| ElemRef(arr, index) -> (
arr[index] := from;
[[s, w], from])
esac
esac
}

fun applySeq(c, first, second) {
case evalList(c, {first, second}) of
[c, {_, result}] -> [c, result]
esac
}

fun applyBinop(c, op, lhs, rhs) {
case evalList(c, {lhs, rhs}) of
[c, {lhsRes, rhsRes}] -> [c, evalOp(op, lhsRes, rhsRes)]
esac
}

fun applyIf(c, condition, thenExpr, elseExpr) {
case eval(c, condition) of
[c, flag] -> if flag then
eval(c, thenExpr)
else
eval(c, elseExpr)
fi
esac
}

fun applyWhile(c@[st, world], condition, body) {
case eval(c, condition) of
[c, flag] -> if flag then
case eval(c, body) of
[c, _] -> applyWhile(c, condition, body)
esac
else
[c, None]
fi
esac
}

fun applyDoWhile(c, condition, body) {
case eval(c, body) of
[c, _] -> applyWhile(c, condition, body)
esac
}

fun applyIgnore(c, expr) {
case eval(c, expr) of
[c, _] -> [c, None]
esac
}

fun evalInScope(c@[s, w], defs, expr) {

fun constructScope(s, defs) {
foldr (fun(s, def) {
case def of
Var(names) -> addNames(s, names)
| Fun(name, args, body) -> addFunction(s, name, args, body)
esac
}, enterScope(s), defs)
}

case defs of
{} -> eval(c, expr)
| _ -> case eval([constructScope(s, defs), w], expr) of
[[s, w], v] -> [[leaveScope(s), w], v]
esac
esac
}

fun applyBuiltin(c@[s, w], name, args) {
case evalList(c, args) of
[[s1, w1], vals] ->
case evalBuiltin(name, vals, w1) of
[value, w] -> [[s1, w], value]
esac
esac
}

fun applyArray(c@[s, w], exprs) {
case evalList(c, exprs) of
[[s, w], vals] -> [[s, w], listArray(vals)]
esac
}

fun applyString(c, literal) {
[c, literal]
}

fun applySexp(c@[s, w], tag, children) {
case evalList(c, children) of
[[s, w], vals] -> [[s, w], Sexp(tag, listArray(vals))]
esac
}

fun applyElem(c, arr, index) {
case evalList(c, {arr, index}) of
[c, {Sexp(tag, children), indexValue}] -> [c, children[indexValue]]
| [c, {arrValue, indexValue}] -> [c, arrValue[indexValue]]
esac
}

fun applyElemRef(c, arr, index) {
case evalList(c, {arr, index}) of
[c, {Sexp(tag, children), indexValue}] -> [c, ElemRef(children, indexValue)]
| [c, {arrValue, indexValue}] -> [c, ElemRef(arrValue, indexValue)]
esac
}

fun applyCall(c@[s, w], name, args) {
fun addArgs(s, args, vals) {
foldr ( fun (s, [arg, value]) {
addName(s, arg, value)
}, s, zip(args, vals))
}

-- printf("call %s on {%s}\n", name, args.string);

case lookup(s, name) of
Fun(formalArgs, External) -> applyBuiltin(c, name, args)
| Fun(formalArgs, body) ->
case evalList(c, args) of
[[s1, w1], vals] ->
case eval([addArgs(enterFunction(s), formalArgs, vals), w], body) of
[[s2, w2], res] -> [[leaveFunction(s1, getGlobal(s2)), w2], res]
esac
esac
esac
}

-- printf("`%s`\n", expr.string);

case expr of
Const(value) -> [c, value]
| Var(name) -> [c, lookup(s, name)]
| Ref(name) -> [c, Ref(name)]
| Binop(op, lhs, rhs) -> applyBinop(c, op, lhs, rhs)
| Skip -> [c, None]
| Assn (lvalue, rvalue) -> applyAssign(c, lvalue, rvalue)
| Set (lvalue, rvalue) -> applyAssign(c, Ref(lvalue), rvalue)
| Seq(first, second) -> applySeq(c, first, second)
| If (condition, thenExpr, elseExpr) -> applyIf(c, condition, thenExpr, elseExpr)
| While (condition, body) -> applyWhile(c, condition, body)
| DoWhile (body, condition) -> applyDoWhile(c, condition, body)
| Ignore (expr) -> applyIgnore(c, expr)
| Scope (defs, expr) -> evalInScope(c, defs, expr)
| Call (name, args) -> applyCall(c, name, args)
| Builtin (name, args) -> applyBuiltin(c, name, args)
| Array(exprs) -> applyArray(c, exprs)
| String (literal) -> applyString(c, literal)
| Elem (arr, index) -> applyElem(c, arr, index)
| ElemRef (arr, index) -> applyElemRef(c, arr, index)
| Sexp (tag, elems) -> applySexp(c, tag, elems)
| _ -> failure("Oops!")
esac
}

-- Evaluates a program with a given input and returns an output
public fun evalExpr (input, expr) {
Expand Down
37 changes: 35 additions & 2 deletions src/Parser.lama
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,33 @@ fun distributeScope (expr, exprConstructor) {
esac
}

var primary = memo $ eta syntax (
var elseIfPart = memo $ eta syntax (
kElif condition=exp kThen body=scopeExpr {[condition, body]}
),
elsePart = memo $ eta syntax (
kElse body=scopeExpr {body}
),
ifExp = memo $ eta syntax (
loc=pos kIf condition=exp kThen body=exp elifs=elseIfPart* els=elsePart? kFi {
fun (a) {
var innerElseBody = case els of
Some(eb) -> eb(a)
| None -> assertVoid(a, Skip, loc)
esac;

var elseBody = foldr (fun(acc, [condition, body]) {
If (condition(Val), body(a), acc)
}, innerElseBody, elifs);

If (condition(Val), body(a), elseBody)
}
}
),
forExp = memo $ eta syntax (
loc=pos kFor pre=scopeExpr s[","] condition=exp s[","] iter=exp kDo body=exp kOd { fun(a) {
assertVoid(a, distributeScope(pre(Void), fun(p) {Seq(p, While(condition(Val), Seq(body(Void), iter(Void))))}), loc)}
}
),primary = memo $ eta syntax (
-- array constant
loc=pos x=inbr[s("["), list0(syntax (e=exp {e(Val)})), s("]")] {fun (a) {assertValue (a, Array (x), loc)}} |

Expand Down Expand Up @@ -147,7 +173,14 @@ var primary = memo $ eta syntax (
None -> {}
| Some (args) -> args
esac), loc)}} |
$(failure ("the rest of primary parsing in not implemented\n"))),
inbr[s("("), scopeExpr, s(")")] |
loc=pos kSkip {fun(a) {assertVoid(a, Skip, loc)}} |
loc=pos kWhile condition=exp kDo body=scopeExpr kOd {fun(a) {assertVoid(a, While(condition(Val), body(Void)), loc)}} |
loc=pos kDo body=scopeExpr kWhile condition=exp kOd { fun(a) {
assertVoid(a, distributeScope(body(Void), fun(b) {DoWhile(b, condition(Val))}), loc)
}} |
forExp |
ifExp ),
basic = memo $ eta (expr ({[Right, {[s (":="),
fun (l, loc, r) {
fun (a) {assertValue (a, Assn (l (Ref), r (Val)), loc)}
Expand Down
Loading
Loading