Skip to content

Commit

Permalink
Merge pull request #26 from chsasank/migrate_brilisp_to_python
Browse files Browse the repository at this point in the history
Migrate brilisp from scheme to python
  • Loading branch information
chsasank authored Jun 10, 2024
2 parents 8609012 + 0dbe339 commit 6f458c4
Show file tree
Hide file tree
Showing 32 changed files with 257 additions and 204 deletions.
173 changes: 173 additions & 0 deletions src/backend/brilisp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
import sys
import json


def is_list(x):
return isinstance(x, list)


def is_function(expr):
return isinstance(expr, list) and (expr[0] == "define")


def gen_function(expr):
header = expr[1]
name = header[0][0]
typ = header[0][1]
args = header[1:]
instrs = expr[2:]
return {
"name": name,
"type": gen_type(typ),
"args": [gen_arg(x) for x in args],
"instrs": [gen_instr(x) for x in instrs],
}


def gen_type(typ):
if is_list(typ):
return {typ[0]: gen_type(typ[1])}
else:
return typ


def gen_arg(arg):
return {"name": arg[0], "type": gen_type(arg[1])}


def gen_instr(instr):
def is_const(instr):
return (instr[0] == "set") and (instr[2][0] == "const")

def gen_constr_instr(instr):
return {
"op": "const",
"type": gen_type(instr[1][1]),
"dest": instr[1][0],
"value": instr[2][1],
}

def is_value(instr):
value_op = {
"add",
"mul",
"sub",
"div",
"eq",
"ne",
"lt",
"gt",
"le",
"ge",
"not",
"and",
"or",
"alloc",
"load",
"ptradd",
"id",
"fadd",
"fsub",
"fmul",
"fdiv",
"feq",
"fne",
"flt",
"fgt",
"fle",
"fge",
}
return (instr[0] == "set") and (instr[2][0] in value_op)

def gen_value_instr(instr):
return {
"op": instr[2][0],
"type": gen_type(instr[1][1]),
"dest": instr[1][0],
"args": instr[2][1:],
}

def is_ret(instr):
return instr[0] == "ret"

def gen_ret_instr(instr):
return {"op": "ret", "args": instr[1:]}

def is_call(instr):
return (instr[0] == "set") and (instr[2][0] == "call")

def gen_call_instr(instr):
return {
"op": "call",
"type": gen_type(instr[1][1]),
"dest": instr[1][0],
"funcs": [instr[2][1]],
"args": instr[2][2:],
}

def is_jmp(instr):
return instr[0] == "jmp"

def gen_jmp_instr(instr):
return {"op": "jmp", "labels": instr[1:]}

def is_label(instr):
return instr[0] == "label"

def gen_label_instr(instr):
return {"label": instr[1]}

def is_br(instr):
return instr[0] == "br"

def gen_br_instr(instr):
return {"op": "br", "args": [instr[1]], "labels": instr[2:]}

def is_nop(instr):
return instr[0] == "nop"

def gen_nop_instr(instr):
return {"op": "nop"}

def is_store(instr):
return instr[0] == "store"

def gen_store_instr(instr):
return {"op": "store", "args": instr[1:]}

if is_const(instr):
return gen_constr_instr(instr)
elif is_value(instr):
return gen_value_instr(instr)
elif is_ret(instr):
return gen_ret_instr(instr)
elif is_call(instr):
return gen_call_instr(instr)
elif is_jmp(instr):
return gen_jmp_instr(instr)
elif is_label(instr):
return gen_label_instr(instr)
elif is_br(instr):
return gen_br_instr(instr)
elif is_nop(instr):
return gen_nop_instr(instr)
elif is_store(instr):
return gen_store_instr(instr)
else:
raise SyntaxError(f"Unknown instruction {instr}")


def brilisp(expr):
for x in expr:
assert is_function(x), f"{x} is not a function"
return {"functions": [gen_function(x) for x in expr]}


def main():
expr = json.load(sys.stdin)
assert expr[0] == "brilisp"
print(json.dumps(brilisp(expr[1:])))


if __name__ == "__main__":
main()
140 changes: 0 additions & 140 deletions src/backend/brilisp.scm

This file was deleted.

4 changes: 2 additions & 2 deletions src/backend/tests/brilisp/access.sexp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(brilisp
(bril-define ((print int) (n int)))
(define ((print int) (n int)))

(bril-define ((main int))
(define ((main int))
(set (inc int) (const 1))
(set (v int) (const 4545454))
(set (max int) (const 8989898))
Expand Down
4 changes: 2 additions & 2 deletions src/backend/tests/brilisp/access_many.sexp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(brilisp
(bril-define ((print int) (n int)))
(define ((print int) (n int)))

(bril-define ((main int))
(define ((main int))
(set (inc int) (const 1))
(set (v int) (const 4545454))
(set (max int) (const 8989898))
Expand Down
4 changes: 2 additions & 2 deletions src/backend/tests/brilisp/access_ptr.sexp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(brilisp
(bril-define ((print int) (n int)))
(define ((print int) (n int)))

(bril-define ((main int))
(define ((main int))
(set (inc int) (const 1))
(set (v int) (const 4545))
(set (max int) (const 8989898))
Expand Down
6 changes: 3 additions & 3 deletions src/backend/tests/brilisp/add-overflow.sexp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(brilisp
(bril-define ((print int) (n int)))
(define ((print int) (n int)))

(bril-define ((pow int) (base int) (ex int))
(define ((pow int) (base int) (ex int))
(set (out int) (const 1))
(set (one int) (const 1))

Expand All @@ -17,7 +17,7 @@
(label ret)
(ret out))

(bril-define ((main int))
(define ((main int))
(set (one int) (const 1))
(set (two int) (const 2))
(set (thiry int) (const 30))
Expand Down
6 changes: 3 additions & 3 deletions src/backend/tests/brilisp/alloc.sexp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(brilisp
(bril-define ((print int) (n int)))
(define ((print int) (n int)))

(bril-define ((print_bool int) (b bool))
(define ((print_bool int) (b bool))
(set (T int) (const 1))
(set (F int) (const 0))
(br b print_true print_false)
Expand All @@ -12,7 +12,7 @@
(set (tmp int) (call print F))
(ret tmp))

(bril-define ((main int))
(define ((main int))
(set (v int) (const 4))
(set (o1 int) (const 1))
(set (bp (ptr bool)) (alloc v))
Expand Down
4 changes: 2 additions & 2 deletions src/backend/tests/brilisp/arr_sum.sexp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(brilisp
(bril-define ((print int) (n int)))
(define ((print int) (n int)))

(bril-define ((main int))
(define ((main int))
(set (len int) (const 10))
(set (arr (ptr int)) (alloc len))
(set (sum int) (const 0))
Expand Down
Loading

0 comments on commit 6f458c4

Please sign in to comment.