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

[CUB] A08 Aleksandr Shefer #1460

Closed
wants to merge 3 commits 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
118 changes: 117 additions & 1 deletion src/Expr.lama
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ fun addNames (state, names) {
foldl (fun (s, name) {s.addName (name, Val (0))}, state, names)
}

fun addNamesVals (state, names, args) {
foldl (fun (s, [name, arg]) {s.addName (name, arg)}, state, zip(names, args))
}

-- Helper function: adds a function in current scope
fun addFunction (state, name, args, body) {
state.addName (name, Fun (args, body))
Expand All @@ -101,8 +105,120 @@ fun evalList (c, exprs) {
esac
}

fun addDefs (state, defs) {
foldl( fun (s, def) {
case def of
Fun (name, args, body) -> addFunction(s, name, args, body)
| Var (vars) -> addNames(s, vars)
esac
}, state, defs)
}

fun eval (c@[s, w], expr) {
failure ("evalExpr not implemented\n")
case expr of
Skip -> [c, None]
| Ignore(e) -> case eval(c, e) of
[c, _] -> [c, None]
esac
| Const(n) -> [c, n]
| Var(x) -> [c, lookup(s, x)]
| Ref(x) -> [c, Ref(x)]
| Binop(op, l, r) ->
case evalList(c, {l, r}) of
[c, {lRes, rRes}] ->
[c, evalOp(op, lRes, rRes)]
esac
| Seq(l, r) ->
case evalList(c, {l, r}) of
[c, {_, rRes}] -> [c, rRes]
esac
| Read(x) ->
case readWorld(w) of
[res, w] -> [[s <- [x, res], w], None]
esac
| Write(e) ->
case eval(c, e) of
[c@[s, w], res] -> [[s, writeWorld(res, w)], None]
esac
| If(cond, l, r) ->
case eval(c, cond) of
[c, resCond] -> eval(c, if resCond then l else r fi)
esac
| While(cond, body) ->
case eval(c, cond) of
[c, resCond] -> if resCond then eval(c, Seq(body, expr)) else [c, None] fi
esac
| DoWhile(body, cond) ->
case eval(c, body) of
[c, _] -> eval(c, While(cond, body))
esac
| Scope(defs, body) ->
case enterScope(s) of
s ->
case addDefs(s, defs) of
s ->
case eval([s, w], body) of
[[sr, wr], res] ->
[[leaveScope(sr), wr], res]
esac
esac
esac
| Call(name, args) ->
case lookup(s, name) of
Fun (names, body) ->
case evalList(c, args) of
[[s, w], vals] ->
case body of
External ->
case evalBuiltin(name, vals, w) of
[res, w] -> [[s, w], res]
esac
| _ ->
case eval([addNamesVals(enterFunction(s), names, vals), w], body) of
[[sr, wr], res] ->
[[leaveFunction(s, getGlobal(sr)), wr], res]
esac
esac
esac
esac
| Assn(x, e) -> -- ?
case evalList(c, {x, e}) of
[c@[s, w], {l, r}] ->
case l of
ElemRef(a, i) ->
[[s, w], a[i] := r]
| Ref(l) ->
[[s <- [l, r], w], r]
esac
esac
| Set(x, expr) -> eval(c, Assn(Ref(x), expr))
| String(s) ->
[c, s]
| Array (elems) ->
case evalList(c, elems) of
[c, vals] -> [c, listArray(vals)]
esac
| Sexp(name, elems) ->
case evalList(c, elems) of
[c, vals] -> [c, Sexp(name, listArray(vals))]
esac
| Elem(a, i) ->
case evalList(c, {a, i}) of
[c, {a, i}] ->
case a of
Sexp(_, a) -> [c, a[i]]
| _ -> [c, a[i]]
esac
esac
| ElemRef(a, i) ->
case evalList(c, {a, i}) of
[c, {a, i}] ->
case a of
Sexp(_, a) -> [c, ElemRef(a, i)]
| _ -> [c, ElemRef(a, i)]
esac
esac
esac
}


Expand Down
30 changes: 27 additions & 3 deletions src/Parser.lama
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ fun distributeScope (expr, exprConstructor) {
esac
}

var parseIf = memo $ eta syntax (
expr=exp kThen s1=scopeExpr kElse s2=scopeExpr kFi { fun(a) { If(expr(Val), s1(a), s2(a)) } } |
expr=exp kThen s1=scopeExpr kFi loc=pos { fun(a) { If(expr(Val), s1(a), assertVoid(a, Skip, loc)) } } |
expr=exp kThen s1=scopeExpr kElif s2=parseIf { fun(a) { If(expr(Val), s1(a), s2(a)) } }
);

var 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 All @@ -131,14 +137,14 @@ var primary = memo $ eta syntax (
loc=pos x=decimal {fun (a) {assertValue (a, Const (stringInt (x)), loc)}} |

-- identifier
x=lident args=inbr[s("("), list0(syntax(e=exp {e(Val)})), s(")")]? {fun (a) {
loc=pos x=lident args=inbr[s("("), list0(syntax(e=exp {e(Val)})), s(")")]? {fun (a) {
case args of
None -> case a of
Ref -> Ref (x)
| Void -> Ignore (Var (x))
| _ -> Var (x)
esac
| Some (args) -> assertValue (a, Call (x, args))
| Some (args) -> assertValue (a, Call (x, args), loc)
esac
}} |

Expand All @@ -147,7 +153,25 @@ 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 kRead x=inbr[s("("), lident, s(")")] { fun(a) { assertVoid(a, Read(x), loc) } } |
-- loc=pos kWrite x=inbr[s("("), exp, s(")")] { fun (a) { assertVoid(a, Write(x(Val)), loc) } } |

kIf ifS=parseIf { fun(a) { ifS(a) } } |

loc=pos kWhile cond=exp kDo body=scopeExpr kOd { fun(a) { assertVoid(a, While(cond(Val), body(Void)), loc)} } |
loc=pos kDo body=scopeExpr kWhile cond=exp kOd {
fun(a) {
assertVoid(a, distributeScope(body(Void), fun (sBody) {DoWhile(sBody, cond(Val))}), loc)
}
} |

loc=pos kFor init=scopeExpr s[","] cond=exp s[","] succ=exp kDo body=scopeExpr kOd {
fun(a) {
assertVoid(a, distributeScope(init(Void), fun (sInit) {Seq(sInit, While(cond(Val), Seq(body(Void), succ(Void))))}), loc)
}
} ),
basic = memo $ eta (expr ({[Right, {[s (":="),
fun (l, loc, r) {
fun (a) {assertValue (a, Assn (l (Ref), r (Val)), loc)}
Expand Down
Loading
Loading