Skip to content

Commit

Permalink
Task 7
Browse files Browse the repository at this point in the history
  • Loading branch information
Perfectrum committed Dec 9, 2023
1 parent f2ac9d9 commit 0bdf774
Show file tree
Hide file tree
Showing 4 changed files with 598 additions and 32 deletions.
99 changes: 98 additions & 1 deletion src/Expr.lama
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,104 @@ fun evalList (c, exprs) {
}

fun eval (c@[s, w], expr) {
failure ("evalExpr not implemented\n")
case expr of
Assn (x, e) ->
case evalList (c, {x, e}) of
[[st, w], {Ref (x), n}] -> [[st <- [x, n], w], n]
| [c, {ElemRef (xs, i), n}] -> [c, xs[i] := n]
esac
| Set (n, e) -> eval (c, Assn (Ref (n), e))
| Seq (l, r) ->
case evalList (c, {l, r}) of
[c, {_, x}] -> [c, x]
esac
| Skip -> [c, Z]
| If (cnd, t, f) ->
case eval (c, cnd) of
[c, cnd] -> if cnd then eval (c, t) else eval (c, f) fi
esac
| While (cnd, b) ->
case eval (c, cnd) of
[c, cnd] -> if cnd then eval (eval (c, b) [0], expr) else [c, Z] fi
esac

| DoWhile (b, cnd) ->
case eval (eval (c, b) [0], cnd) of
[c, cnd] -> if cnd then eval (c, expr) else [c, Z] fi
esac

| Var (n) -> [c, lookup (s, n)]
| Ref (n) -> [c, Ref (n)]
| Const (x) -> [c, x]
| Binop (op, le, re) ->
case evalList (c, {le, re}) of
[c, {le, re}] -> [c, evalOp (op, le, re)]
esac
| Ignore (e) -> case eval (c, e) of [c, _] -> [c, Z] esac
| Scope (ds, expr) ->
case eval (
[ foldl (
fun (s, d) {
case d of
Var (names) -> addNames (s, names)
| Fun (name, args, body) -> addFunction (s, name, args, body)
esac
}, s.enterScope, ds
),
w ], expr) of
[ [s, w], n] ->
[ [leaveScope (s), w], n]
esac
| Call (funName, argExprs) ->
case lookup (s, funName) of
Fun (argNames, bodyExpr) ->
case evalList (c, argExprs)
of [[sNew, w], argVals] ->
case bodyExpr of
External ->
case evalBuiltin (funName, argVals, w) of
[res, w] -> [[s, w], res]
esac
| _ ->
case eval (
[ foldl (
fun (s, [name, v]) { addName (s, name, v) },
enterFunction (sNew),
zip (argNames, argVals)
),
w ], bodyExpr) of
[ [sNew, w], res ] ->
[ [leaveFunction (s, getGlobal (sNew)), w ], res]
esac
esac
esac
esac
| String (s) -> [c, s]
| Array (es) ->
case evalList (c, es) of
[c, vals] ->
[c, listArray (vals)]
esac
| Sexp (s, es) ->
case evalList (c, es) of
[c, vals] ->
[c, Sexp (s, listArray (vals))]
esac
| Elem (e, ie) ->
case evalList (c, {e, ie}) of
[ c, {xs, i} ] ->
case xs of
Sexp (_, xs) -> [ c, xs [i] ]
| _ -> [ c, xs [i] ]
esac
esac
| ElemRef (e, ie) ->
case evalList (c, {e, ie}) of
[c, {xs, i}] ->
[ c, ElemRef (xs, i) ]
esac

esac
}


Expand Down
29 changes: 28 additions & 1 deletion src/Parser.lama
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,34 @@ 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 cnd=scopeExpr kDo wrk=scopeExpr kOd { fun (a) { assertVoid (a, While (cnd (Val), wrk (Void)), loc) } } |
loc=pos kDo wrk=scopeExpr kWhile cnd=scopeExpr kOd { fun (a) { assertVoid ( a, distributeScope (wrk (Void), fun (wrk) { DoWhile (wrk, cnd (Val)) }), loc ) } } |
loc=pos kFor init=scopeExpr s[","] cnd=scopeExpr s[","] inc=scopeExpr kDo wrk=scopeExpr kOd {
fun (a) {
assertVoid (
a,
distributeScope ( init (Void), fun (init) { Seq (init, While (cnd (Val), Seq (wrk (Void), inc (Void)))) } ),
loc
)
} } |

kIf wrk=ifBody {wrk}
),
ifBody = memo $ eta syntax (
cnd=exp kThen thn=exp els=ifTail { fun (a) { If (cnd (Val), thn (a), els (a)) } }
),
ifTail = memo $ eta syntax (
kElif wrk=ifBody {wrk}
| kElse wrk=exp kFi {wrk}
| kFi { fun (a) { assertVoid (a, Skip, loc) } }
),





basic = memo $ eta (expr ({[Right, {[s (":="),
fun (l, loc, r) {
fun (a) {assertValue (a, Assn (l (Ref), r (Val)), loc)}
Expand Down
Loading

0 comments on commit 0bdf774

Please sign in to comment.