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

[NUP] A06 Vsevolod Vaskin #1406

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
85 changes: 76 additions & 9 deletions src/Expr.lama
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,8 @@ public fun evalOp (op, l, r) {
-- Ref (string) |
-- Const (int) |
-- Binop (string, expr, expr) |
-- Scope (def list, expr) |
-- Call (string, expr list) |
-- Ignore (expr)
--
-- A definition is either a local variable definition or a function
-- definition:
--
-- def = Var (string list) |
-- Fun (string, string list, expr)

-- Helper function: checks that given name designates a regular variable in
-- a given state
Expand Down Expand Up @@ -101,9 +94,83 @@ fun evalList (c, exprs) {
esac
}

(* Assignment *)
fun addDefinitions(s, defs) {
foldl(fun (cs, def) {
case def of
Var(x) -> addNames(cs, x)
| Fun(name, args, body) -> addFunction(cs, name, args, body)
esac
}, s, defs)
}

fun addValues(state, args, vals) {
fun myAddName(s, [x, v]) {
addName(s, x, Var(v))
}
foldl(myAddName, state, zip(args, vals))
}

fun eval (c@[s, w], expr) {
failure ("evalExpr not implemented\n")
case expr of
Var (x) -> case lookup(s, x) of
Var (v) -> [c, v]
| t -> failure("Var expected, got %s", string(t))
esac
| Ref (x) -> [c, x]
| Const (x) -> [c, x]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrap in Var

| Skip -> [c, Void]
| Seq (s1, s2) -> case eval(c, s1) of
[c, _] -> eval(c, s2)
esac
| Read (x) -> case readWorld(w) of
[v, w] -> [[s <- [x, Var(v)], w], Void]
esac
| Write (e) -> case eval(c, e) of
[[s, w], value] -> [[s, writeWorld(value, w)], Void]
esac
| If (e, s1, s2) ->
case eval(c, e) of
[c, 0] -> eval(c, s2)
| [c, _] -> eval(c, s1)
esac
| Ignore (e) -> case eval(c, e) of
[c, _] -> [c, Void]
esac
| Assn(e1, e2) -> case eval(c, e2) of
[c, e2Val] -> case eval(c, e1) of
[[s, w], e1Val] -> [[s <- [e1Val, Var(e2Val)], w], e2Val]
esac
esac
| While (exp, stmt) -> case eval(c, exp) of
[c, 0] -> [c, Void]
| [c, _] -> eval(c, Seq (stmt, While (exp, stmt)))
esac
| DoWhile (e1, e) -> eval (c, Seq (e1, While (e, e1)))
| Binop (op, e1, e2) ->
case eval (c, e1) of [c, v1] ->
case eval (c, e2) of [c, v2] ->
[c, evalOp (op, v1, v2)]
esac esac
| Call(name, args) -> case lookup(s, name) of
Fun(fArgs, body) -> case evalList(c, args) of
[[stOut, w], vals] -> case enterFunction(stOut) of
stIn -> case addValues(stIn, fArgs, vals) of
stIn -> case eval([stIn, w], body) of
[[stIn, w], value] -> [[leaveFunction(stOut, getGlobal(stIn)), w], value]
esac
esac
esac
esac
esac
| Scope(defs, exp) -> case enterScope(s) of
s -> case addDefinitions(s, defs) of
s -> case eval([s, w], exp) of
[[s, w], v] -> [[leaveScope(s), w], v]
esac
esac
esac
| x -> failure ("Cannot match expression %s\n", x.string)
esac
}
(* End *)

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

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

var primary = memo $ eta syntax (
-- decimal constant
loc=pos x=decimal {fun (a) {assertValue (a, Const (stringInt (x)), loc)}} |
Expand All @@ -117,12 +123,30 @@ var primary = memo $ eta syntax (
| Void -> Ignore (Var (x))
| _ -> Var (x)
esac
| Some (args) -> assertValue (a, Call (x, args))
| Some (args) -> assertValue (a, Call (x, args), loc)
esac
}} |
(* Assignment *)
$(failure ("the rest of primary parsing in not implemented\n"))),
(* End *)
inbr[s("("), scopeExpr ,s(")")] |
loc=pos kRead x=inbr[s("("), lident, s(")")] { fun(a) { assertValue(a, Read(x), loc) } } |
loc=pos kWrite x=inbr[s("("), exp, s(")")] { fun(a) { assertVoid(a, Write(x(Val)), loc) } } |
loc=pos kSkip { fun(a) { assertVoid(a, Skip, loc) } } |
loc=pos kWhile e=exp kDo s=scopeExpr kOd { fun(a) {assertVoid(a, While(e(Val), s(Void)), loc)} } |
loc=pos kDo s=scopeExpr kWhile e=exp kOd { fun(a) { assertVoid(a,
case s(Void) of
Scope(defs, body) -> Scope(defs, DoWhile(body, e(Val)))
| body -> DoWhile(body, e(Val))
esac
, loc)
} } |
loc=pos kFor s1=scopeExpr s[","] e=exp s[","] s2=exp kDo s3=scopeExpr kOd { fun(a) { assertVoid(a,
case s1(Void) of
Scope(defs, s1) -> Scope(defs, Seq(s1, While (e(Val), Seq(s3(Void), s2(Void)))))
| s1 -> Seq(s1, While (e(Val), Seq(s3(Void), s2(Void))))
esac
, loc) } } |
loc=pos kIf e=exp kThen s1=scopeExpr s2=afterIf { fun(a) { If (e(Val), s1(a), s2(a)) } }
),

basic = memo $ eta (expr ({[Right, {[s (":="),
fun (l, loc, r) {
fun (a) {assertValue (a, Assn (l (Ref), r (Val)), loc)}
Expand Down Expand Up @@ -154,4 +178,4 @@ var primary = memo $ eta syntax (
exp = memo $ eta syntax (basic | s1=basic s[";"] s2=exp {fun (a) {Seq (s1 (Void), s2 (a))}});

-- Public top-level parser
public parse = syntax (s=scopeExpr {s (Void)});
public parse = syntax (s=scopeExpr {s (Void)});
Loading
Loading