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

[SPBGU] A03 Ilia Bondarenko #1473

Open
wants to merge 2 commits into
base: A03-straight-line-parser
Choose a base branch
from
Open
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
23 changes: 22 additions & 1 deletion src/Expr.lama
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,26 @@ import State;
-- Binop (string, expr, expr)

public fun evalExpr (st, expr) {
failure ("evalExpr not implemented\n")
case expr of
Var (varStr) -> st (varStr)
| Const (num) -> num
| Binop (op, left, right) ->
var left_res = evalExpr(st, left);
var right_res = evalExpr(st, right);
case op of
"+" -> left_res + right_res
| "-" -> left_res - right_res
| "*" -> left_res * right_res
| "/" -> left_res / right_res
| "%" -> left_res % right_res
| "==" -> left_res == right_res
| "!=" -> left_res != right_res
| "<" -> left_res < right_res
| "<=" -> left_res <= right_res
| ">" -> left_res > right_res
| ">=" -> left_res >= right_res
| "&&" -> left_res && right_res
| "!!" -> left_res !! right_res
esac
esac
}
36 changes: 34 additions & 2 deletions src/Parser.lama
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,45 @@ fun inbr (l, p, r) {
syntax (-l p -r)
}

fun binop (l, op, r) {
Binop (op, l, r)
}

-- Primary expression
var primary = memo $ eta syntax (x=decimal {Const (stringInt (x))} |
x=lident {Var (x)} |
inbr[s("("), exp, s(")")]),
exp = memo $ eta (failure ("expression parsing not implemented\n"));
exp = memo $ eta expr ({
[Left, { [s ("!!"), binop],
[s ("&&"), binop] }],
[Nona, { [s ("!="), binop],
[s ("=="), binop],
[s (">"), binop],
[s ("<"), binop],
[s (">="), binop],
[s ("<="), binop] }],
[Left, { [s ("+"), binop],
[s ("-"), binop] }],
[Left, { [s ("*"), binop],
[s ("/"), binop],
[s ("%"), binop] }]
},
primary
);

var stGeneral = memo $ eta syntax (
kRead x=inbr[s("("), lident, s(")")] {Read (x)}
| kWrite w=inbr[s("("), exp, s(")")] {Write (w)}
| kSkip {Skip}
| x=lident s[":="] w=exp { Assn (x, w) }
);

var stmt = memo $ eta (failure ("statement parsing not implemented\n"));
var stmt = memo $ eta syntax (
stGeneral |
g=stGeneral s[";"] t=stmt {
Seq(g, t)
}
);


-- Public top-level parser
Expand Down
65 changes: 59 additions & 6 deletions src/SM.lama
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,48 @@ public fun showSM (prg) {

-- Stack machine interpreter. Takes an SM-configuration and a program,
-- returns a final configuration
fun eval (c, insns) {
failure ("SM eval not implemented\n")
fun eval (c@[stack, state, world], insns) {
case insns of
{} -> [stack, state, world]
| start:end ->
case start of
READ ->
case readWorld(world) of [inst, world] ->
eval ([inst:stack, state, world], end)
esac
| WRITE ->
case stack of inst:stack ->
eval([stack, state, writeWorld(inst, world)], end)
esac
| BINOP (op) ->
case stack of right:left:stack ->
var ans = case op of
"+" -> left + right
| "-" -> left - right
| "*" -> left * right
| "/" -> left / right
| "!=" -> left != right
| "&&" -> left && right
| "!!" -> left !! right
| "%" -> left % right
| "==" -> left == right
| "<" -> left < right
| "<=" -> left <= right
| ">" -> left > right
| ">=" -> left >= right
esac;
eval([ans:stack, state, world], end)
esac
| LD (varStr) ->
eval ([state (varStr):stack, state, world], end)
| ST (varStr) ->
case stack of inst:stack ->
eval ([stack, state <- [varStr, inst], world], end)
esac
| CONST (num) ->
eval ([num:stack, state, world], end)
esac
esac
}

-- Runs a stack machine for a given input and a given program, returns an output
Expand All @@ -36,15 +76,28 @@ public fun evalSM (input, insns) {
}

-- Compiles an expression into a stack machine code.
-- Takes an expression, returns a list
-- of stack machine instructions
-- Takes an expression, returns a list of stack machine instructions
fun compileExpr (expr) {
failure ("compileExpr not implemented\n")
case expr of
Var(varStr) -> {
LD (varStr)
}
| Const (num) -> {
CONST (num)
}
| Binop (op, l, r) -> compileExpr (l) +++ compileExpr (r) +++ { BINOP (op) }
esac
}

-- Compiles a statement into a stack machine code.
-- Takes a statement, returns a list of stack machine
-- instructions.
public fun compileSM (stmt) {
failure ("compileSM not implemented\n")
case stmt of
Assn (a, exp) -> compileExpr (exp) +++ ( ST (a) : {})
| Seq (fst, snd) -> compileSM (fst) +++ compileSM (snd)
| Skip -> {}
| Read (x) -> READ : ST (x) : {}
| Write (exp) -> compileExpr (exp) +++ (WRITE : {})
esac
}
18 changes: 17 additions & 1 deletion src/Stmt.lama
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,23 @@ import World;
-- Write (expr) |

fun eval (c, stmt) {
failure ("Stmt eval not implemented\n")
case stmt of
Assn (varStr, exp) ->
case c of [state, world] -> [state <- [varStr, evalExpr (state, exp)], world]
esac
| Seq (l, r) -> eval(eval (c, l), r)
| Skip -> c
| Read (varStr) ->
case c of [state, world] ->
case readWorld (world) of [a, wrld] ->
[state <- [varStr, a], wrld]
esac
esac
| Write (exp) ->
case c of [state, world] ->
[state, writeWorld(evalExpr (state, exp), world)]
esac
esac
}

-- Evaluates a program with a given input and returns an output
Expand Down
52 changes: 50 additions & 2 deletions src/X86.lama
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,24 @@ fun suffix (op) {
esac
}

fun opers (op) {
case op of
"+" -> Regular
| "-" -> Regular
| "*" -> Regular
| "/" -> Divide
| "!!" -> Binary
| "&&" -> Binary
| "%" -> DivRemainder
| "<" -> Comparison
| ">" -> Comparison
| "!=" -> Comparison
| ">=" -> Comparison
| "<=" -> Comparison
| "==" -> Comparison
esac
}

-- Compiles stack machine code into a list of x86 instructions. Takes an environment
-- and stack machine code, returns an updated environment and x86 code.
fun compile (env, code) {
Expand All @@ -288,7 +306,37 @@ fun compile (env, code) {
case env.pop of
[s, env] -> [env, code <+ Push (s) <+ Call ("Lwrite") <+ Pop (eax)]
esac
| _ -> failure ("codegeneration for instruction %s is not yet implemented\n", i.string)
| LD (x) ->
case env.addGlobal (x).allocate of
[s, env] -> [env, code <+> move (env.loc (x), s)]
esac
| ST (x) ->
case env.addGlobal (x).pop of
[s, env] -> [env, code <+> move (s, env.loc (x))]
esac
| CONST (x) ->
case env.allocate of
[s, env] -> [env, code <+> move (L (x), s)]
esac
| BINOP (op) ->
case env.pop2 of [fst, snd, env] ->
case env.allocate of [t, env] ->
case opers(op) of
Regular -> [env, code <+> move(snd, eax) <+
Binop (op, fst, eax) <+> move (eax, t)]
| Divide -> [env, code <+> move(snd, eax) <+
Cltd <+ IDiv (fst) <+> move (eax, t)]
| DivRemainder -> [env, code <+> move(snd, eax) <+
Cltd <+ IDiv (fst) <+> move (edx, t)]
| Comparison -> [env, code <+ Binop ("^", eax, eax) <+> move(snd, edx) <+
Binop ("cmp", fst, edx) <+ Set (suffix (op), "%al") <+> move (eax, t)]
| Binary -> [env, code <+ Binop ("^", eax, eax) <+
Binop ("cmp", L (0), snd) <+ Set (suffix ("!="), "%al") <+> move (eax, t) <+
Binop ("^", eax, eax) <+ Binop ("cmp", L (0), fst) <+
Set (suffix ("!="), "%al") <+ Binop (op, eax, t)]
esac
esac
esac
esac
}, [env, emptyBuffer ()], code)
}
Expand Down Expand Up @@ -319,4 +367,4 @@ public fun compileX86 (args, code) {

system ({"gcc -g -m32 -o ", args.getBaseName, " ", runtime, " ", asmFile}.stringcat)
esac
}
}
Loading