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

A08 Matskevich Valery #1204

Closed
wants to merge 10 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
18 changes: 9 additions & 9 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ jobs:
else
echo ${branch:1:2} > assignment_number.txt
fi
- name: Check Deadline
run: |
deadline=2023-12-30T23:59
if [[ $(date +'%Y-%m-%dT%H:%M') > $deadline ]];
then
echo "FIASCO: The deadline has expired"
echo $(date +'%Y-%m-%dT%H:%M')
exit 1
fi
- name: Check Deadline
run: |
deadline=2023-12-30T23:59
if [[ $(date +'%Y-%m-%dT%H:%M') > $deadline ]];
then
echo "FIASCO: The deadline has expired"
echo $(date +'%Y-%m-%dT%H:%M')
exit 1
fi
- uses: actions/upload-artifact@v2
with:
name: share_info
Expand Down
87 changes: 86 additions & 1 deletion src/Expr.lama
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,92 @@ fun evalList (c, exprs) {
}

fun eval (c@[s, w], expr) {
failure ("evalExpr not implemented\n")
fun defineSymbols (st, definitions) {
case definitions of
head : tail -> case head of
Fun (fn, argList, expr) -> defineSymbols (st.addFunction (fn, argList, expr), tail)
| Var (nameList) -> defineSymbols (st.addNames (nameList), tail)
esac
| _ -> st
esac
}

fun setArgs (st, args) {
case args of
[argId, argVal] : tail -> setArgs (st.addName (argId, argVal), tail)
| _ -> st
esac
}

case expr of
Assn (lhs, rhs) -> case evalList(c, {lhs, rhs}) of
[[s, w], {lvalue, rvalue}] -> case lvalue of
ElemRef (arr, idx) -> [[s, w], arr[idx] := rvalue]
| _ -> [[s <- [lvalue, rvalue], w], rvalue]
esac
esac
| Set (name, expr) -> case eval (c, expr) of
[[s, w], res] -> [[s <- [name, res], w], res]
esac
| Seq (expr1, expr2) -> case evalList (c, {expr1, expr2}) of
[c, {_, tail}] -> [c, tail]
esac
| Skip -> [c, Void]
| If (cond, expr1, expr2) -> case eval (c, cond) of
[c, 0] -> eval (c, expr2)
| [c, _] -> eval (c, expr1)
esac
| While (cond, expr) -> case eval (c, cond) of
[c, 0] -> [c, Void]
| [c, _] -> eval (c, Seq (expr, While (cond, expr)))
esac
| DoWhile (expr, cond) -> eval (c, Seq (expr, While (cond, expr)))
| Var (name) -> [c, s.lookup (name)]
| Ref (name) -> [c, name]
| Const (value) -> [c, value]
| Binop (kind, lhs, rhs) -> case evalList (c, {lhs, rhs}) of
[c, {lhs, rhs}] -> [c, evalOp (kind, lhs, rhs)]
esac
| Scope (defList, expr) -> case eval ([defineSymbols (s.enterScope, defList), w], expr) of
[[s, w], tail] -> [[s.leaveScope, w], tail]
esac
| Call (fn, passList) -> case s.lookup (fn) of
Fun (_, External) -> eval (c, Builtin (fn, passList))
| Fun (argList, expr) -> case evalList (c, passList) of
[[sOld, w], argValues] ->
case setArgs (sOld.enterFunction, zip (argList, argValues)) of
s -> case eval ([s, w], expr) of
[[s, w], tail] ->
[[sOld.leaveFunction (s.getGlobal), w], tail]
esac
esac
esac
esac
| Ignore (expr) -> case eval (c, expr) of
[c, _] -> [c, Void]
esac
| Array (exprs) -> case evalList (c, exprs) of
[c, values] -> [c, listArray (values)]
esac
| Builtin (name, args) -> case evalList (c, args) of
[[s, w], argValues] -> case evalBuiltin (name, argValues, w) of
[res, w] -> [[s, w], res]
esac
esac
| Elem (arrExpr, idxExpr) -> case evalList (c, {arrExpr, idxExpr}) of
[c, {arr, idx}] -> case arr of
Sexp (name, arr) -> [c, arr[idx]]
| _ -> [c, arr[idx]]
esac
esac
| ElemRef (arrExpr, idxExpr) -> case evalList (c, {arrExpr, idxExpr}) of
[c, {arr, idx}] -> [c, ElemRef (arr, idx)]
esac
| Sexp (name, args) -> case evalList (c, args) of
[c, values] -> [c, Sexp (name, listArray (values))]
esac
| String (cstr) -> [c, cstr]
esac
}


Expand Down
80 changes: 50 additions & 30 deletions src/Parser.lama
Original file line number Diff line number Diff line change
Expand Up @@ -118,36 +118,56 @@ fun distributeScope (expr, exprConstructor) {
}

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)}} |

-- string constant
loc=pos x=strlit {fun (a) {assertValue (a, String (x), loc)}} |

-- character literal
loc=pos x=chrlit {fun (a) {assertValue (a, Const (x), loc)}} |

-- decimal constant
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) {
case args of
None -> case a of
Ref -> Ref (x)
| Void -> Ignore (Var (x))
| _ -> Var (x)
esac
| Some (args) -> assertValue (a, Call (x, args))
esac
}} |

-- S-expression
loc=pos x=uident args=inbr[s("("), list0(syntax(e=exp {e(Val)})), s(")")]? {fun (a) {assertValue (a, Sexp (x, case args of
None -> {}
| Some (args) -> args
esac), loc)}} |
$(failure ("the rest of primary parsing in not implemented\n"))),
-- array constant
loc=pos x=inbr[s("["), list0(syntax (e=exp {e(Val)})), s("]")] {fun (a) {assertValue (a, Array (x), loc)}} |

-- string constant
loc=pos x=strlit {fun (a) {assertValue (a, String (x), loc)}} |

-- character literal
loc=pos x=chrlit {fun (a) {assertValue (a, Const (x), loc)}} |

-- decimal constant
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) {
case args of
None -> case a of
Ref -> Ref (x)
| Void -> Ignore (Var (x))
| _ -> Var (x)
esac
| Some (args) -> assertValue (a, Call (x, args))
esac
}} |

-- S-expression
loc=pos x=uident args=inbr[s("("), list0(syntax(e=exp {e(Val)})), s(")")]? {fun (a) {assertValue (a, Sexp (x, case args of
None -> {}
| Some (args) -> args
esac), loc)}} |
inbr[s("("), scopeExpr, s(")")] |
loc=pos kSkip { fun (a) { assertVoid (a, Skip, loc) } } |
loc=pos kWhile cond=exp kDo expr=scopeExpr kOd { fun (a) {assertVoid (a, While (cond (Val), expr (Void)), loc)}} |
loc=pos kDo expr=scopeExpr kWhile cond=exp kOd { fun (a) {assertVoid (a, case expr (Void) of
Scope (d, s) ->
Scope(d, DoWhile (s, cond (Val)))
| _ ->
DoWhile (expr (Void), cond (Val))
esac, loc)}} |
loc=pos kFor ini=scopeExpr s[","] cond=exp s[","] step=exp kDo expr=exp kOd { fun (a) {
assertVoid (a, case ini (Void) of
Scope (d, ini) -> Scope (d, Seq (ini, While (cond (Val), Seq (expr (Void), step (Void)))))
| _ -> Seq (ini (Void), While (cond (Val), Seq (expr (Void), step (Void))))
esac, loc)}} |
loc=pos kIf cond=exp kThen expr1=exp kElse expr2=exp kFi { fun (a) {If (cond (Val), expr1 (a), expr2 (a))} } |
loc=pos kIf cond=exp kThen expr=exp eliftok=elifs { fun (a) {If (cond (Val), expr (a), eliftok (a))} }),
elifs = memo $ eta syntax (
loc=pos kFi { fun (a) { assertVoid(a, Skip, loc) }} |
loc=pos kElif cond=exp kThen expr1=exp kElse expr2=exp kFi { fun (a) { If (cond (Val), expr1 (a), expr2 (a)) }} |
loc=pos kElif cond=exp kThen expr=exp eliftok=elifs { fun (a) { If (cond (Val), expr (a), eliftok (a)) }}
),
basic = memo $ eta (expr ({[Right, {[s (":="),
fun (l, loc, r) {
fun (a) {assertValue (a, Assn (l (Ref), r (Val)), loc)}
Expand Down
Loading
Loading