diff --git a/src/backend/brilisp.py b/src/backend/brilisp.py index 47c6da0..2309128 100644 --- a/src/backend/brilisp.py +++ b/src/backend/brilisp.py @@ -158,15 +158,16 @@ def gen_store_instr(instr): def brilisp(expr): - for x in expr: + assert expr[0] == "brilisp" + body = expr[1:] + for x in body: assert is_function(x), f"{x} is not a function" - return {"functions": [gen_function(x) for x in expr]} + return {"functions": [gen_function(x) for x in body]} def main(): expr = json.load(sys.stdin) - assert expr[0] == "brilisp" - print(json.dumps(brilisp(expr[1:]))) + print(json.dumps(brilisp(expr))) if __name__ == "__main__": diff --git a/src/backend/c-lisp.py b/src/backend/c-lisp.py index 2a0e3a9..6f69e57 100755 --- a/src/backend/c-lisp.py +++ b/src/backend/c-lisp.py @@ -14,34 +14,40 @@ class BrilispCodeGenerator: def __init__(self): # Type tracking self.symbol_types = {} # Variable name -> type + self.scopes = [] # Stack of scope tags self.function_types = {} # Function name > (ret-type, (arg-types...)) + self.pointer_types = {} # For internal use, e.g. temporary pointer variables - self.binary_op_types = { + self.fixed_op_types = { # : # Integer arithmetic - "add": "int", - "sub": "int", - "mul": "int", - "div": "int", + "add": ("int", 2), + "sub": ("int", 2), + "mul": ("int", 2), + "div": ("int", 2), # Integer comparison - "eq": "bool", - "ne": "bool", - "lt": "bool", - "gt": "bool", - "le": "bool", - "ge": "bool", + "eq": ("bool", 2), + "ne": ("bool", 2), + "lt": ("bool", 2), + "gt": ("bool", 2), + "le": ("bool", 2), + "ge": ("bool", 2), # Floating-point arithmetic - "fadd": "float", - "fsub": "float", - "fmul": "float", - "fdiv": "float", + "fadd": ("float", 2), + "fsub": ("float", 2), + "fmul": ("float", 2), + "fdiv": ("float", 2), # Floating-point comparison - "feq": "bool", - "fne": "bool", - "flt": "bool", - "fgt": "bool", - "fle": "bool", - "fge": "bool", + "feq": ("bool", 2), + "fne": ("bool", 2), + "flt": ("bool", 2), + "fgt": ("bool", 2), + "fle": ("bool", 2), + "fge": ("bool", 2), + # Boolean logic + "and": ("bool", 2), + "or": ("bool", 2), + "not": ("bool", 1), } def c_lisp(self, prog): @@ -51,6 +57,18 @@ def c_lisp(self, prog): return ["brilisp"] + [self.gen_function(fn) for fn in prog[1:]] + def construct_scoped_name(self, name, scopes): + return ".".join([name] + scopes) + + def scoped_lookup(self, name): + """Look up the name in reverse order of current scope stack""" + # be as specific as possible first + for s in range(len(self.scopes), -1, -1): + scoped_name = self.construct_scoped_name(name, self.scopes[:s]) + if scoped_name in self.symbol_types: + return scoped_name + raise CodegenError(f"Undeclared symbol: {name}") + def gen_function(self, func): if not func[0] == "define": raise CodegenError(f"Not a function: {func}") @@ -59,7 +77,10 @@ def gen_function(self, func): if not len(elem) == 2: raise CodegenError(f"Bad function prototype: {func[1]}") - self.symbol_types = {} # Clear the symbol table + # Clear the symbol table and scope stack + self.symbol_types = {} + self.scopes = [] + name, ret_type = func[1][0] parm_types = [] for parm in func[1][1:]: @@ -69,7 +90,7 @@ def gen_function(self, func): return [ "define", func[1], - *self.gen_stmt(func[2:]), + *self.gen_compound_stmt(func[2:], new_scope=False), ] def gen_stmt(self, stmt): @@ -87,6 +108,8 @@ def gen_stmt(self, stmt): return self.gen_if_stmt(stmt) elif self.is_for_stmt(stmt): return self.gen_for_stmt(stmt) + elif self.is_while_stmt(stmt): + return self.gen_while_stmt(stmt) else: return self.gen_expr(stmt) else: @@ -95,6 +118,35 @@ def gen_stmt(self, stmt): print(f"Error in statement: {stmt}") raise e + def is_while_stmt(self, stmt): + return stmt[0] == "while" + + def gen_while_stmt(self, stmt): + if len(stmt) < 3: + raise CodegenError(f"Bad while statement: {stmt}") + + cond_sym, loop_lbl, cont_lbl, break_lbl = [ + random_label(CLISP_PREFIX, [extra]) + for extra in ( + "cond", + "loop", + "cont", + "break", + ) + ] + cond_expr_instr = self.gen_expr(stmt[1], res_sym=cond_sym) + loop_stmt_instr = self.gen_stmt(stmt[2:]) + + return [ + ["label", loop_lbl], + *cond_expr_instr, + ["br", cond_sym, cont_lbl, break_lbl], + ["label", cont_lbl], + *loop_stmt_instr, + ["jmp", loop_lbl], + ["label", break_lbl], + ] + def is_for_stmt(self, stmt): return stmt[0] == "for" @@ -170,9 +222,10 @@ def gen_decl_stmt(self, stmt): raise CodegenError(f"bad declare statement: {stmt}") name, typ = stmt[1] - if name in self.symbol_types: + scoped_name = self.construct_scoped_name(name, self.scopes) + if scoped_name in self.symbol_types: raise CodegenError(f"Re-declaration of variable {name}") - self.symbol_types[name] = typ + self.symbol_types[scoped_name] = typ return [] def is_ret_stmt(self, stmt): @@ -194,10 +247,15 @@ def gen_ret_stmt(self, stmt): def is_compound_stmt(self, stmt): return isinstance(stmt, list) and isinstance(stmt[0], list) - def gen_compound_stmt(self, stmt): + def gen_compound_stmt(self, stmt, new_scope=True): + if new_scope: + scope = random_label() + self.scopes.append(scope) instr_list = [] for s in stmt: instr_list += self.gen_stmt(s) + if new_scope: + self.scopes.pop() return instr_list def is_set_expr(self, expr): @@ -205,11 +263,14 @@ def is_set_expr(self, expr): def gen_set_expr(self, expr, res_sym): name = expr[1] - if not name in self.symbol_types: + scoped_name = self.scoped_lookup(name) + if not scoped_name in self.symbol_types: raise CodegenError(f"Cannot set undeclared variable: {name}") instr_list = self.gen_expr(expr[2], res_sym=res_sym) - instr_list.append(["set", [name, self.symbol_types[name]], ["id", res_sym]]) + instr_list.append( + ["set", [scoped_name, self.symbol_types[scoped_name]], ["id", res_sym]] + ) return instr_list def get_literal_type(self, expr): @@ -248,28 +309,96 @@ def is_var_expr(self, expr): return isinstance(expr, str) def gen_var_expr(self, expr, res_sym): - if expr in self.symbol_types: - return [["set", [res_sym, self.symbol_types[expr]], ["id", expr]]] + scoped_name = self.scoped_lookup(expr) + if scoped_name in self.symbol_types: + typ = self.symbol_types[scoped_name] + instr_list = [["set", [res_sym, typ], ["id", scoped_name]]] + if typ[0] == "ptr": + self.pointer_types[res_sym] = typ + return instr_list else: raise CodegenError(f"Reference to undeclared variable: {expr}") - def is_binary_expr(self, expr): - return expr[0] in self.binary_op_types - - def gen_binary_expr(self, expr, res_sym): - if not len(expr) == 3: - raise CodegenError(f"Binary operation takes only 2 operands: {expr}") + def is_fixed_type_expr(self, expr): + return expr[0] in self.fixed_op_types + def gen_fixed_type_expr(self, expr, res_sym): instr_list = [] - in1_sym, in2_sym = [ - random_label(CLISP_PREFIX, [extra]) for extra in ("in1", "in2") - ] opcode = expr[0] - typ = self.binary_op_types[opcode] + typ, n_ops = self.fixed_op_types[opcode] + if not (len(expr) == n_ops + 1): + raise CodegenError(f"`{opcode}` takes only 2 operands: {expr}") + in_syms = [ + random_label(CLISP_PREFIX, [f"inp_{n}"]) for n in range(n_ops) + ] + input_instrs = [] + for n in range(n_ops): + input_instrs += [*self.gen_expr(expr[n + 1], in_syms[n])] + return [ + *input_instrs, + ["set", [res_sym, typ], [opcode, *in_syms]], + ] + + def is_ptradd_expr(self, expr): + return expr[0] == "ptradd" + + def gen_ptradd_expr(self, expr, res_sym): + if len(expr) != 3: + raise CodegenError(f"Bad ptradd expression: {expr}") + + offset_sym = random_label(CLISP_PREFIX) + ptr_name = self.scoped_lookup(expr[1]) + ptr_type = self.symbol_types[ptr_name] + self.pointer_types[res_sym] = ptr_type + return [ + *self.gen_expr(expr[2], res_sym=offset_sym), + ["set", [res_sym, ptr_type], ["ptradd", ptr_name, offset_sym]], + ] + + def is_load_expr(self, expr): + return expr[0] == "load" + + def gen_load_expr(self, expr, res_sym): + if len(expr) != 2: + raise CodegenError(f"Bad load expression: {expr}") + + ptr_sym = random_label(CLISP_PREFIX) + return [ + *self.gen_expr(expr[1], res_sym=ptr_sym), + ["set", [res_sym, self.pointer_types[ptr_sym][1]], ["load", ptr_sym]], + ] + + def is_store_expr(self, expr): + return expr[0] == "store" + + def gen_store_expr(self, expr, res_sym): + if len(expr) != 3: + raise CodegenError(f"Bad store expression: {expr}") + + val_sym, ptr_sym = [ + random_label(CLISP_PREFIX, [extra]) + for extra in ("val", "ptr") + ] + return [ + *self.gen_expr(expr[1], res_sym=ptr_sym), + *self.gen_expr(expr[2], res_sym=val_sym), + ["store", ptr_sym, val_sym], + ["set", [res_sym, self.pointer_types[ptr_sym][1]], ["id", val_sym]], + ] + + def is_alloc_expr(self, expr): + return expr[0] == "alloc" + + def gen_alloc_expr(self, expr, res_sym): + if len(expr) != 3: + raise CodegenError(f"Bad alloc expression: {expr}") + + ptr_type = ["ptr", expr[1]] + self.pointer_types[res_sym] = ptr_type + size_sym = random_label(CLISP_PREFIX) return [ - *self.gen_expr(expr[1], in1_sym), - *self.gen_expr(expr[2], in2_sym), - ["set", [res_sym, typ], [opcode, in1_sym, in2_sym]], + *self.gen_expr(expr[2], res_sym=size_sym), + ["set", [res_sym, ptr_type], ["alloc", size_sym]], ] def gen_expr(self, expr, res_sym=None): @@ -282,8 +411,16 @@ def gen_expr(self, expr, res_sym=None): return self.gen_call_expr(expr, res_sym) elif self.is_var_expr(expr): return self.gen_var_expr(expr, res_sym) - elif self.is_binary_expr(expr): - return self.gen_binary_expr(expr, res_sym) + elif self.is_fixed_type_expr(expr): + return self.gen_fixed_type_expr(expr, res_sym) + elif self.is_ptradd_expr(expr): + return self.gen_ptradd_expr(expr, res_sym) + elif self.is_load_expr(expr): + return self.gen_load_expr(expr, res_sym) + elif self.is_store_expr(expr): + return self.gen_store_expr(expr, res_sym) + elif self.is_alloc_expr(expr): + return self.gen_alloc_expr(expr, res_sym) else: raise CodegenError(f"Bad expression: {expr}") diff --git a/src/backend/tests/brilisp/run.sh b/src/backend/tests/brilisp/run.sh index a4b4466..3dde5cd 100644 --- a/src/backend/tests/brilisp/run.sh +++ b/src/backend/tests/brilisp/run.sh @@ -6,6 +6,6 @@ tmp_out=$(mktemp --suffix '.out') cp /dev/stdin $tmp_in clang $tmp_in runtime.c -o $tmp_out -Wno-override-module -O2 -$tmp_out +$tmp_out $@ rm $tmp_in $tmp_out diff --git a/src/backend/tests/c-lisp/array-sum.out b/src/backend/tests/c-lisp/array-sum.out new file mode 100644 index 0000000..daca503 --- /dev/null +++ b/src/backend/tests/c-lisp/array-sum.out @@ -0,0 +1,2 @@ +45 +285 diff --git a/src/backend/tests/c-lisp/array-sum.sexp b/src/backend/tests/c-lisp/array-sum.sexp new file mode 100644 index 0000000..a21b213 --- /dev/null +++ b/src/backend/tests/c-lisp/array-sum.sexp @@ -0,0 +1,34 @@ +(c-lisp + (define ((print int) (n int))) + + (define ((arr_sum int) (a (ptr int)) (n int)) + (declare (i int)) + (declare (sum int)) + (set sum 0) + + (for ((set i 0) + (lt i n) + (set i (add i 1))) + (set sum (add sum (load (ptradd a i))))) + (ret sum)) + + (define ((main void)) + (declare (arr1 (ptr int))) + (declare (arr2 (ptr int))) + (declare (i int)) + + (set arr1 (alloc int 10)) + (set arr2 (alloc int 10)) + + (for ((set i 0) + (lt i 10) + (set i (add i 1))) + (declare (arr_i (ptr int))) + (set arr_i (ptradd arr1 i)) + (store arr_i i) + (set arr_i (ptradd arr2 i)) + (store arr_i (mul i i))) + + (call print (call arr_sum arr1 10)) + (call print (call arr_sum arr2 10)) + (ret))) diff --git a/src/backend/tests/c-lisp/bisection-rootfind.sexp b/src/backend/tests/c-lisp/bisection-rootfind.sexp index 1aa3445..f725b3a 100644 --- a/src/backend/tests/c-lisp/bisection-rootfind.sexp +++ b/src/backend/tests/c-lisp/bisection-rootfind.sexp @@ -19,10 +19,8 @@ (declare (mid-pt float)) (declare (loop bool)) - (for ((set loop #t); TODO: Use `while` and `break` once implemented - loop - 0) - + (set loop #t) + (while loop (set mid-pt (fdiv (fadd pos-pt neg-pt) 2.0)) (set mid-val diff --git a/src/backend/tests/c-lisp/gaussian-elimination.out b/src/backend/tests/c-lisp/gaussian-elimination.out new file mode 100644 index 0000000..89a610f --- /dev/null +++ b/src/backend/tests/c-lisp/gaussian-elimination.out @@ -0,0 +1,9 @@ +1.000000 +-1.000000 +1.000000 +0.000000 +1.000000 +-12.000000 +0.000000 +0.000000 +57.000000 diff --git a/src/backend/tests/c-lisp/gaussian-elimination.sexp b/src/backend/tests/c-lisp/gaussian-elimination.sexp new file mode 100644 index 0000000..8533139 --- /dev/null +++ b/src/backend/tests/c-lisp/gaussian-elimination.sexp @@ -0,0 +1,82 @@ +;; ARGS: 3 3 1 -1 1 3 -2 -9 2 3 -1 +(c-lisp + (define ((print int) (n int))) + (define ((fprint float) (n float))) + (define ((strtof float) (s (ptr int)) (end int))) + (define ((atoi int) (s (ptr int)))) + + (define ((row-op void) (mod (ptr float)) (ref (ptr float)) (len int)) ; mod -> mod - factor*ref + (declare (i int)) + (declare (mod_p (ptr float))) + (declare (l float)) + (declare (pv int)) + + (for ((set pv 0) + (and + (feq (load (ptradd mod pv)) 0.0) + (lt pv len)) + (set pv (add pv 1))) + ()) + + (if (lt pv len) + (set l + (fdiv + (load (ptradd mod pv)) + (load (ptradd ref pv)))) + (ret)) + + (for ((set i pv) + (lt i len) + (set i (add i 1))) + (store + (set mod_p (ptradd mod i)) + (fsub + (load mod_p) + (fmul l (load (ptradd ref i)))))) + ;(call fprint (load mod_p)) + (ret)) + + (define ((gaussian-eli void) (mat (ptr float)) (nrows int) (ncols int)) + (declare (ref-row int)) + (declare (mod-row int)) + (declare (col int)) + + (for ((set ref-row 0) + (lt ref-row (sub nrows 1)) + (set ref-row (add ref-row 1))) + (for ((set mod-row (add ref-row 1)) + (lt mod-row nrows) + (set mod-row (add mod-row 1))) + (call row-op + (ptradd mat (mul ncols mod-row)) + (ptradd mat (mul ncols ref-row)) + ncols))) + + (ret)) + + (define ((main void) (argc int) (argv (ptr (ptr int)))) + (declare (input (ptr float))) + (declare (nr int)) + (declare (nc int)) + (declare (sz int)) + (declare (i int)) + + (set nr (call atoi (load (ptradd argv 1)))) + (set nc (call atoi (load (ptradd argv 2)))) + (set sz (mul nr nc)) + (set input (alloc float sz)) + + (for ((set i 0) + (lt i sz) + (set i (add i 1))) + (store + (ptradd input i) + (call strtof (load (ptradd argv (add i 3))) 0))) + + (call gaussian-eli input nr nc) + (for ((set i 0) + (lt i sz) + (set i (add i 1))) + (call fprint (load (ptradd input i)))) + + (ret))) diff --git a/src/backend/tests/c-lisp/insertion-sort.out b/src/backend/tests/c-lisp/insertion-sort.out new file mode 100644 index 0000000..cd67bc9 --- /dev/null +++ b/src/backend/tests/c-lisp/insertion-sort.out @@ -0,0 +1,11 @@ +-19 +-7 +-5 +-2 +1 +2 +3 +8 +23 +56 +78 diff --git a/src/backend/tests/c-lisp/insertion-sort.sexp b/src/backend/tests/c-lisp/insertion-sort.sexp new file mode 100644 index 0000000..8af8e6c --- /dev/null +++ b/src/backend/tests/c-lisp/insertion-sort.sexp @@ -0,0 +1,64 @@ +;; ARGS: 8 23 1 56 2 -5 -2 78 3 -7 -19 + +(c-lisp + (define ((print int) (n int))) + (define ((atoi int) (s (ptr int)))) + + (define ((swap void) (a (ptr int)) (b (ptr int))) + (declare (tmp int)) + (set tmp (load b)) + (store b (load a)) + (store a tmp) + (ret)) + + (define ((min int) (arr (ptr int)) (n int)) + (declare (i int)) + (declare (idx int)) + + (set idx 0) + (for ((set i 1) + (lt i n) + (set i (add i 1))) + (if (lt (load (ptradd arr i)) (load (ptradd arr idx))) + (set idx i))) + (ret idx)) + + (define ((insertion-sort void) (arr (ptr int)) (len int)) + (declare (min-idx int)) + (declare (i int)) + (declare (rest-arr (ptr int))) + + (for ((set i 0) + (lt i (sub len 1)) + (set i (add i 1))) + (set rest-arr (ptradd arr i)) + (set min-idx (call min rest-arr (sub len i))) + (if (not (eq min-idx 0)) + (call swap + rest-arr + (ptradd rest-arr min-idx)))) + (ret)) + + (define ((main void) (argc int) (argv (ptr (ptr int)))) + (declare (input (ptr int))) + (declare (len int)) + (declare (i int)) + + (set len (sub argc 1)) + (set input (alloc int len)) + + (for ((set i 0) + (lt i len) + (set i (add i 1))) + (store + (ptradd input i) + (call atoi (load (ptradd argv (add i 1)))))) + + (call insertion-sort input len) + + (for ((set i 0) + (lt i len) + (set i (add i 1))) + (call print (load (ptradd input i)))) + (ret))) + diff --git a/src/backend/tests/c-lisp/matrix-sum.out b/src/backend/tests/c-lisp/matrix-sum.out new file mode 100644 index 0000000..5c0a7d7 --- /dev/null +++ b/src/backend/tests/c-lisp/matrix-sum.out @@ -0,0 +1,16 @@ +22 +24 +26 +28 +42 +44 +46 +48 +62 +64 +66 +68 +82 +84 +86 +88 diff --git a/src/backend/tests/c-lisp/matrix-sum.sexp b/src/backend/tests/c-lisp/matrix-sum.sexp new file mode 100644 index 0000000..4f75b70 --- /dev/null +++ b/src/backend/tests/c-lisp/matrix-sum.sexp @@ -0,0 +1,64 @@ +(c-lisp + (define ((malloc (ptr int)) (bytes int))) + (define ((free void) (arr (ptr int)))) + (define ((print int) (n int))) + + (define ((index int) (i int) (j int) (len int)) + (ret (add (mul i len) j))) + + (define ((mat-cons void) (arr (ptr int)) (len int)) + (declare (i int)) + (declare (j int)) + + (for ((set i 0) + (lt i len) + (set i (add i 1))) + (for ((set j 0) + (lt j len) + (set j (add j 1))) + (store + (ptradd arr (call index i j len)) + (add 11 (add (mul i 10) j))))) + (ret)) + + (define ((mat-add (ptr int)) (a (ptr int)) (b (ptr int)) (len int)) + (declare (i int)) + (declare (j int)) + (declare (idx int)) + + (declare (res (ptr int))) + (set res (call malloc (mul (mul len len) 4))) + + (for ((set i 0) + (lt i len) + (set i (add i 1))) + (for ((set j 0) + (lt j len) + (set j (add j 1))) + (set idx (call index i j len)) + (store + (ptradd res idx) + (add + (load (ptradd a idx)) + (load (ptradd b idx)))))) + (ret res)) + + (define ((main void)) + (declare (in1 (ptr int))) + (declare (in2 (ptr int))) + (declare (out (ptr int))) + + (set in1 (alloc int 16)) + (call mat-cons in1 4) + (set in2 (alloc int 16)) + (call mat-cons in2 4) + + (set out (call mat-add in1 in2 4)) + + (declare (i int)) + (for ((set i 0) + (lt i 16) + (set i (add i 1))) + (call print (load (ptradd out i)))) + (call free out) + (ret))) diff --git a/src/backend/tests/c-lisp/scope.out b/src/backend/tests/c-lisp/scope.out new file mode 100644 index 0000000..4adacaf --- /dev/null +++ b/src/backend/tests/c-lisp/scope.out @@ -0,0 +1,2 @@ +45 +0 diff --git a/src/backend/tests/c-lisp/scope.sexp b/src/backend/tests/c-lisp/scope.sexp new file mode 100644 index 0000000..c189fb3 --- /dev/null +++ b/src/backend/tests/c-lisp/scope.sexp @@ -0,0 +1,22 @@ +(c-lisp + (define ((print int) (n int))) + + (define ((main void)) + (declare (sum1 int)) + (set sum1 0) + (declare (sum2 int)) + (set sum2 0) + + (declare (same-name int)) + + (for ((set same-name 0) + (lt same-name 10) + (set same-name (add same-name 1))) + ((declare (same-name int)) + (set same-name 0) + (set sum2 (add sum2 same-name))) + (set sum1 (add sum1 same-name))) + + (call print sum1) + (call print sum2) + (ret))) diff --git a/src/backend/tests/c-lisp/transpose.out b/src/backend/tests/c-lisp/transpose.out new file mode 100644 index 0000000..6a37be3 --- /dev/null +++ b/src/backend/tests/c-lisp/transpose.out @@ -0,0 +1,25 @@ +11 +21 +31 +41 +51 +12 +22 +32 +42 +52 +13 +23 +33 +43 +53 +14 +24 +34 +44 +54 +15 +25 +35 +45 +55 diff --git a/src/backend/tests/c-lisp/transpose.sexp b/src/backend/tests/c-lisp/transpose.sexp new file mode 100644 index 0000000..47af23f --- /dev/null +++ b/src/backend/tests/c-lisp/transpose.sexp @@ -0,0 +1,58 @@ +(c-lisp + (define ((print int) (n int))) + + (define ((swap void) (a (ptr int)) (b (ptr int))) + (declare (tmp int)) + (set tmp (load b)) + (store b (load a)) + (store a tmp) + (ret)) + + (define ((index int) (i int) (j int) (len int)) + (ret (add (mul i len) j))) + + (define ((transpose void) (mat (ptr int)) (len int)) + (declare (i int)) + (declare (j int)) + + (for ((set i 0) + (lt i len) + (set i (add i 1))) + (for ((set j (add i 1)) + (lt j len) + (set j (add j 1))) + (call swap (ptradd mat (call index i j len)) (ptradd mat (call index j i len))))) + (ret)) + + (define ((mat-cons void) (arr (ptr int)) (len int)) + (declare (i int)) + (declare (j int)) + + (for ((set i 0) + (lt i len) + (set i (add i 1))) + (for ((set j 0) + (lt j len) + (set j (add j 1))) + (store + (ptradd arr (call index i j len)) + (add 11 (add (mul i 10) j))))) + (ret)) + + (define ((arr-print void) (arr (ptr int)) (len int)) + (declare (i int)) + + (for ((set i 0) + (lt i len) + (set i (add i 1))) + (call print (load (ptradd arr i)))) + (ret)) + + (define ((main void)) + (declare (input (ptr int))) + + (set input (alloc int 25)) + (call mat-cons input 5) + (call transpose input 5) + (call arr-print input 25) + (ret))) diff --git a/src/backend/tests/c-lisp/turnt.toml b/src/backend/tests/c-lisp/turnt.toml index acf020c..5d75021 100644 --- a/src/backend/tests/c-lisp/turnt.toml +++ b/src/backend/tests/c-lisp/turnt.toml @@ -1 +1 @@ -command = "guile ../../utils/sexp-json.scm < {filename} | python ../../c-lisp.py | python ../../brilisp.py | python ../../llvm.py | bash ../brilisp/run.sh " +command = "guile ../../utils/sexp-json.scm < {filename} | python ../../c-lisp.py | python ../../brilisp.py | python ../../llvm.py | bash ../brilisp/run.sh {args}" diff --git a/src/backend/utils/random.py b/src/backend/utils/random.py index 086942f..5958317 100644 --- a/src/backend/utils/random.py +++ b/src/backend/utils/random.py @@ -2,13 +2,14 @@ import string -def random_label(prefix="", extra_prefixes=[], length=10): +def random_label(prefix="", extra_prefixes=[], length=8): """ Return a random string. If prefix and extra_prefix are given, they will be used as prefixes to the random string. """ - return ( - ".".join([prefix, *extra_prefixes]) - + "." - + "".join([random.choice(string.ascii_lowercase) for i in range(length)]) - ) + prefixes = ".".join([prefix, *extra_prefixes]) + suffix = "".join([random.choice(string.ascii_lowercase) for i in range(length)]) + if prefixes: + return f"{prefixes}-{suffix}" + else: + return suffix