Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
77e6725
extra file
Sep 21, 2020
b330af8
Add HW 2
Sep 21, 2020
96d29b2
rm extra
Sep 28, 2020
1f84c5d
hw3
Sep 28, 2020
4afa526
rm extra
Sep 28, 2020
6baedb2
rm extra
Sep 28, 2020
babaf40
Fixed typo in X86 comment
dboulytchev Sep 29, 2020
99ec445
Fixed typo in X86 comment
dboulytchev Sep 29, 2020
f95b010
Removed Util
dboulytchev Sep 30, 2020
28e9d32
Removed Util
dboulytchev Sep 30, 2020
46e4b4b
add gitignore
Oct 1, 2020
243fff0
Update SM.lama
danyaberezun Oct 1, 2020
8352586
hw4
Oct 1, 2020
fed16b4
realize interpreter
igoroogle Oct 2, 2020
94279bb
add compiler
igoroogle Oct 2, 2020
4b719fa
resolve conflicts
igoroogle Oct 2, 2020
cb5229d
resolve conflicts
igoroogle Oct 2, 2020
3040a29
Merge branch 'A01-straight-line-int-sm' of https://github.com/igoroog…
igoroogle Oct 2, 2020
22d2755
edit drafts
igoroogle Oct 2, 2020
548dec5
add hw descr
Oct 5, 2020
bba45f2
add lecture notes pdf
Oct 5, 2020
b420834
rm extra files
Oct 12, 2020
446de7f
Merge branch 'A03-straight-line-parser' of github.com:danyaberezun/co…
Oct 12, 2020
136aae6
FIxed buffer initialization
dboulytchev Oct 24, 2020
4878e9e
add merges
igoroogle Dec 29, 2020
cc5399a
change SM and add x86
igoroogle Dec 29, 2020
9e56020
add merge
igoroogle Dec 29, 2020
383676c
change x86 and parser
igoroogle Dec 31, 2020
b399a36
try to do it
igoroogle Dec 31, 2020
6a79a2e
upd all
igoroogle Feb 2, 2021
1c2cbf6
merge all again
igoroogle Feb 2, 2021
64a44a1
add hw5 changes to merge in repo
igoroogle Feb 2, 2021
f5383f7
restore Makefile
igoroogle Feb 2, 2021
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*
!src/*.lama
!.gitignore
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
# compiler-2020
A supplementary repository for the course on compilers.

ДЗ 4 (Конструкции управления):

* Написать синтаксический анализатор для- и поддержать в режими интерпретации конструкции потока управления `if`, `while`, `for`, `repeat ... until`
* `if...elif...[else ...]?` и `for` должны быть реализованы как синтасический сахар
* `repeat ... until` необходимо поддержать на уровне абстрактного синтаксиса
Binary file added lectures/control_flow_interpr.pdf
Binary file not shown.
80 changes: 80 additions & 0 deletions regression/Embedding.meta
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
-- A deep embedding of L0 into Lama
import List;
import Array;
import Fun;
import World;
import Stmt;
import State;
import SM;
import X86;

-- Embeds expression operands: strings are
-- embedded into identifiers, integer constants --- into
-- constants; non-operand expressions are left intact

# define HASH #

fun opnd (x) {
case x of
HASH string -> Var (x)
| HASH unboxed -> Const (x)
| _ -> x
esac
}

-- Redefinition of standard infix operators
infix + at + (l, r) {Binop ("+", opnd (l), opnd (r))}
infix - at - (l, r) {Binop ("-", opnd (l), opnd (r))}
infix * at * (l, r) {Binop ("*", opnd (l), opnd (r))}
infix / at / (l, r) {Binop ("/", opnd (l), opnd (r))}
infix % at % (l, r) {Binop ("%", opnd (l), opnd (r))}
infix == at == (l, r) {Binop ("==", opnd (l), opnd (r))}
infix != at != (l, r) {Binop ("!=", opnd (l), opnd (r))}
infix < at < (l, r) {Binop ("<", opnd (l), opnd (r))}
infix <= at <= (l, r) {Binop ("<=", opnd (l), opnd (r))}
infix > at > (l, r) {Binop (">", opnd (l), opnd (r))}
infix >= at >= (l, r) {Binop (">=", opnd (l), opnd (r))}
infix && at && (l, r) {Binop ("&&", opnd (l), opnd (r))}
infix !! at !! (l, r) {Binop ("!!", opnd (l), opnd (r))}

-- Embeds "read" construct; x is expected to be a string (not a "Var")
fun read (x) {
Read (x)
}

-- Embeds "write" construct; note, e is expression, thus it
-- is embedded using "opnd"
fun write (e) {
Write (opnd (e))
}

-- Embeds assignment operator; x is expected to be string, e is expression, this
-- it is embeeded using "opnd"
infix ::= before := (x, e) {
Assn (x, opnd (e))
}

-- Embeds sequential composition
infixr >> before ::= (s1, s2) {
Seq (s1, s2)
}

-- Returns embedded program (thus, the file has to be recompiled as the
-- program changes)
fun program () {
PROGRAM_BODY
}

case sysargs of
[_] -> printf ("%s\n", compileX86 (compileSM (program ())))
| _ ->
local input =
reverse (fix (fun (f) {fun (acc) {case readLine () of HASH unboxed -> acc | arg -> f (stringInt (arg) : acc) esac}}) ({}));

iter (fun (x) {printf ("%d\n", x)},
case sysargs[1] of
"-i" -> evalStmt (input, program ())
| "-s" -> evalSM (input, compileSM (program ()))
esac
)
esac
2 changes: 1 addition & 1 deletion regression/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ expr_tests:
clean:
$(RM) *.s *.i *~ $(LOGS) $(TESTS) *.run
make -C expressions clean
make -C deep-expressions clean
make -C deep-expressions clean
30 changes: 8 additions & 22 deletions src/Driver.lama
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,8 @@ import Manifest;
-- defined in the unit Manifest
fun parseArgs (args) {
local mode = ref (Comp),
infile = ref ({}),
smDump = ref (false);
infile = ref ({});

fun setDump (m) {
case m of
SM -> smDump ::= true
esac
}

fun setMode (m) {
case deref (mode) of
Comp -> mode ::= m
Expand All @@ -47,36 +40,29 @@ fun parseArgs (args) {
{} -> skip
| h : t ->
case h of
"-i" -> setMode (Int)
| "-s" -> setMode (SM)
| "-ds" -> setDump (SM)
| fn -> setInFile (fn)
"-i" -> setMode (Int)
| "-s" -> setMode (SM)
| fn -> setInFile (fn)
esac;
rec (t)
esac
}
})(args);


[fun () {deref (mode)},
fun () {case deref(infile) of #unboxed -> failure ("input file name not set\n") | fn -> fn esac},
fun () {deref (smDump)}
fun () {case deref(infile) of #unboxed -> failure ("input file name not set\n") | fn -> fn esac}
]
}

-- Utility function to peeping to SM code
fun peepSM (args, smCode) {
dumpSM (args, lazy (showSM (smCode)));
smCode
}

local args = parseArgs (arrayList (sysargs).tl);

-- The main part: parses input file, invokes interpreter/stack machine interpreter/x86
-- code generator
case parseString (parse |> bypass (end), fread (args.getInFile)) of
Succ (program) ->
case args.getMode of
Comp -> compileX86 (args, peepSM (args, compileSM (program)))
Comp -> compileX86 (args, compileSM (program))
| mode ->
local input =
reverse (fix (fun (f) {
Expand All @@ -90,7 +76,7 @@ case parseString (parse |> bypass (end), fread (args.getInFile)) of
iter (fun (x) {printf ("%d\n", x)},
case mode of
Int -> evalStmt (input, program)
| SM -> evalSM (input, peepSM (args, compileSM (program)))
| SM -> evalSM (input, compileSM (program))
esac)
esac
| x@Fail (err, line, col) ->
Expand Down
27 changes: 25 additions & 2 deletions src/Expr.lama
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,29 @@ import State;
-- Const (int) |
-- Binop (string, expr, expr)

public fun evalExpr (st, expr) {
failure ("evalExpr not implemented\n")
public fun evalOp (op, expr1, expr2) {
case op of
"+" -> expr1 + expr2 |
"-" -> expr1 - expr2 |
"*" -> expr1 * expr2 |
"/" -> expr1 / expr2 |
"%" -> expr1 % expr2 |
"==" -> expr1 == expr2 |
"!=" -> expr1 != expr2 |
"<" -> expr1 < expr2 |
"<=" -> expr1 <= expr2 |
">" -> expr1 > expr2 |
">=" -> expr1 >= expr2 |
"&&" -> expr1 && expr2 |
"!!" -> expr1 !! expr2
esac
}


public fun evalExpr (st, expr) {
case expr of
Var (x) -> st(x) |
Const (n) -> n |
Binop (op, expr1, expr2) -> evalOp(op, evalExpr(st, expr1), evalExpr(st, expr2))
esac
}
3 changes: 1 addition & 2 deletions src/Lexer.lama
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,4 @@ public kRead = s (rRead),
| a -> a
esac)
}
});

});
3 changes: 1 addition & 2 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,4 @@ X86.o: SM.o Manifest.o
$(LAMAC) -I . -c $<

clean:
rm -Rf *.s *.o *.i *~ *.html *.sm lama-impl

rm -Rf *.s *.o *.i *~ *.html *.sm lama-impl
8 changes: 0 additions & 8 deletions src/Manifest.lama
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
-- Manifests a top-level environment
import Lazy;
import Fun;

public fun getMode (args) {
args [0] ()
Expand All @@ -10,12 +8,6 @@ public fun getInFile (args) {
args [1] ()
}

public fun dumpSM (args, smCode) {
if args [2] () then
fwrite (args.getBaseName ++ ".sm", force $ smCode)
fi
}

public fun getBaseName (args) {
force (lazy ({local name = args.getInFile;
if (matchSubString (name, ".lama", name.length - 5))
Expand Down
45 changes: 38 additions & 7 deletions src/Parser.lama
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,44 @@ fun inbr (l, p, r) {
}

-- Primary expression
local primary = memo $ eta (decimal @ fun (x) {Const (stringInt (x))} |
lident @ fun (x) {Var (x)} |
inbr (s ("("), exp, s (")"))),
exp = memo $ eta (failure ("expression parsing not implemented\n"));
local primary = memo $ eta syntax (x=decimal {Const (stringInt (x))} |
x=lident {Var (x)} |
inbr[s("("), exp, s(")")]),


binOps = fun(l, op, r) {Binop(op, l, r)},
ops = {
[Left, [s ("!!"), binOps] : {}],
[Left, [s ("&&"), binOps] : {}],
[Nona, [s ("<") | s (">") | s ("<=") | s (">=") | s ("==") | s ("!="), binOps] : {}],
[Left, [s ("+") | s ("-"), binOps] : {}],
[Left, [s ("*") | s ("/") | s ("%"), binOps] : {}]
},

exp = memo $ eta expr (ops, primary) ;

local stmt = memo $ eta (failure ("statement parsing not implemented\n"));


local stmt = memo $ eta syntax (
kWrite x=inbr[s("("), exp, s(")")] {Write (x)} |
kRead x=inbr[s("("), lident, s(")")] {Read (x)} |
x=lident s[":="] e=exp {Assn (x, e)} |
stmt1=stmt s[";"] stmt2=stmt end {Seq (stmt1, stmt2)}|

kSkip {Skip} |
kIf cond=exp kThen s1=seqq s2=ifTailParser {If (cond, s1, s2)} |
kWhile cond=exp kDo s=seqq kOd {While (cond, s)} |
kRepeat s=seqq kUntil cond=exp {Repeat (s, cond)} |
kFor e1=seqq s[","] e2=exp s[","] e3=seqq kDo s=seqq kOd {Seq (e1, While(e2, Seq(s, e3)))}
),
ifTailParser = memo $ eta syntax(
-kFi {Skip} |
-kElse seqq -kFi |
-kElif cond=exp kThen s1=seqq s2=ifTailParser {If (cond, s1, s2)}
),

seqq = memo $ eta syntax (stmt | s1=stmt s[";"] s2=seqq {Seq(s1, s2)});



-- Public top-level parser
public parse = stmt;
public parse = stmt;
Loading