diff --git a/LRM.pdf b/LRM.pdf new file mode 100644 index 0000000..108c371 Binary files /dev/null and b/LRM.pdf differ diff --git a/README.txt b/README.txt index b696859..e54b34d 100644 --- a/README.txt +++ b/README.txt @@ -1,4 +1,6 @@ # GAL Authors: Macrina Maria Lobo, Donovan Chan, Anton Nefedenkov, Andrew Feather Graph Application Language + +A toy language we implemented to study compilers. July 2016 diff --git a/fail_assignment_list1.diff b/fail_assignment_list1.diff deleted file mode 100644 index d63f1c1..0000000 --- a/fail_assignment_list1.diff +++ /dev/null @@ -1,6 +0,0 @@ -1,2c1 -< Fatal error: exception Failure(" -< in main expr: illegal assignment to variable l1") ---- -> Fatal error: exception Failure("statement not implemented") -\ No newline at end of file diff --git a/fail_assignment_list1.err b/fail_assignment_list1.err deleted file mode 100644 index 926b4d4..0000000 --- a/fail_assignment_list1.err +++ /dev/null @@ -1,2 +0,0 @@ -Fatal error: exception Failure(" - in main expr: illegal assignment to variable l1") diff --git a/src/ast.ml b/src/ast.ml index d7dc8e5..1de8d89 100644 --- a/src/ast.ml +++ b/src/ast.ml @@ -3,8 +3,8 @@ Note: This code was writte on top of Prof. Edwards's microc code. We hope this is acceptable. *) -type op = Add | Sub | Mult | Div | Equal | - Less | Leq | Greater | Geq | And | Or +type op = Add | Sub | Mult | Div | Equal | Neq | + Less | Leq | Greater | Geq | And | Or (* List and Edge here are different from below *) type uop = Not diff --git a/src/codegen.ml b/src/codegen.ml index 910d93c..e1ab8bf 100644 --- a/src/codegen.ml +++ b/src/codegen.ml @@ -12,13 +12,13 @@ module StringMap = Map.Make(String) let translate (globals, functions) = - (* let contains s1 s2 = - let re = Str.regexp_string s2 - in - try ignore (Str.search_forward re s1 0); true - with Not_found -> false - - in *) + let the_funcs_map = StringMap.empty in + let the_funcs_map = + List.fold_left + (fun map fdecl -> StringMap.add fdecl.A.fname fdecl.A.typ map) + the_funcs_map + functions + in (* Holding global string constants *) let glob_str_const_hash = Hashtbl.create 200 in @@ -52,13 +52,6 @@ let translate (globals, functions) = let n_node_t = L.named_struct_type context "nnode" in L.struct_set_body n_node_t (Array.of_list [L.pointer_type n_node_t; L.pointer_type e_node_t; i32_t ]) true; - -(* let Some(node_t) = L.type_by_name the_module "node" in - let Some(e_node_t) = L.type_by_name the_module "enode" in - let Some(i_node_t) = L.type_by_name the_module "inode" in - let Some(n_node_t) = L.type_by_name the_module "nnode" in - let Some(empty_node_t) = L.type_by_name the_module "empty" in *) - (* Pattern match on A.typ returning a llvm type *) let ltype_of_typ ltyp = match ltyp with @@ -180,6 +173,23 @@ let translate (globals, functions) = | A.Id(name) -> let ocaml_type = (Hashtbl.find ocaml_local_hash name) in list_type_from_type ocaml_type + | A.Call("iadd", _) | A.Call("inext", _) -> + i_node_t + | A.Call("eadd", _) | A.Call("enext", _) -> + e_node_t + | A.Call("sadd", _) | A.Call("snext", _) -> + node_t + | A.Call("nadd", _) | A.Call("nnext", _) -> + n_node_t + | A.Call("ilength", _) | A.Call("slength", _) | A.Call("nlength", _) | A.Call("elength", _) -> + i_node_t + | A.Call(fname, _) -> + let ftype = StringMap.find fname the_funcs_map in + ltype_of_typ ftype + (* try let fdecl = List.find + (fun fdecl -> if fdecl.A.fname = fname then true else false) + functions + in (ltype_of_typ fdecl.A.typ) with Not_found -> in *) | _ -> raise (Failure(" type not supported in list ")) @@ -210,11 +220,14 @@ let translate (globals, functions) = in match e with | A.Litint(i) -> L.const_int i32_t i | A.Litstr(str) -> - let s = L.build_global_stringptr str "" builder in + let s = L.build_global_stringptr str str builder in let zero = L.const_int i32_t 0 in - let lvalue = L.build_in_bounds_gep s [|zero|] "" builder in + let lvalue = L.build_in_bounds_gep s [|zero|] str builder in + let lv_str = L.string_of_llvalue s in + (* P.fprintf stderr "%s\n" lv_str; *) + Hashtbl.add glob_str_const_hash lvalue str; - lvalue + s | A.Edgedcl(src, w, dst) -> let src_p = expr builder src and w = expr builder w @@ -266,10 +279,6 @@ let translate (globals, functions) = let loc_var = lookup name in let e' = (expr builder e) in - (* let node_t_str = L.string_of_lltype (L.type_of loc_var) in - let loc_var_str = L.string_of_lltype (L.type_of e') in - P.fprintf stderr "%s and %s\n" node_t_str loc_var_str; *) - (* Cant add it like this. Need a different comparison. And need to remove old var form the hash map *) if ((L.pointer_type empty_node_t) = (L.type_of e')) then @@ -321,7 +330,7 @@ let translate (globals, functions) = | A.Call("dest", [e]) -> let dest_field_pointer = L.build_struct_gep (expr builder e) 2 "" builder in L.build_load dest_field_pointer "" builder - | A.Call("spop", [e]) -> + | A.Call("spop", [e]) | A.Call("epop", [e]) | A.Call("ipop", [e]) | A.Call("npop", [e])-> let head_node_p = (expr builder e) in let head_node_next_node_pointer = L.build_struct_gep head_node_p 0 "" builder in ignore (L.build_free head_node_p builder); @@ -383,20 +392,19 @@ let translate (globals, functions) = (* Attach the new head to the old head *) add_element the_head new_node - | A.Call("str_comp", [s1;s2]) -> + | A.Call("streq", [s1;s2]) -> let v1 = (expr builder s1) and v2 = expr builder s2 in - let v1str = Hashtbl.find glob_str_const_hash v1 - and v2str = Hashtbl.find glob_str_const_hash v2 in - - (* let space = Str.regexp_string " " in - - let tokenize s = Str.search_forward per_regex s 1 in - - let Some(v1name_pos) = get_name v1str and Some(v2name_pos) = get_name v2str in *) + let v1value = L.build_load (L.build_load (L.global_initializer v1) "" builder) "" builder in + let v2value = L.build_load (L.build_load (L.global_initializer v2) "" builder) "" builder in - P.fprintf stderr "%s and %s\n" v1str v2str; - L.const_int i32_t 0 + let str = L.string_of_lltype (L.type_of v2value) in + let result = (L.build_icmp L.Icmp.Eq v1value v2value "" builder) in + let result = L.build_not result "" builder in + let result = L.build_intcast result i32_t "" builder in + result + (* + *) | A.Call(fname, actuals) -> (* Will clean up later *) @@ -426,6 +434,7 @@ let translate (globals, functions) = | A.And -> L.build_and | A.Or -> L.build_or | A.Equal -> L.build_icmp L.Icmp.Eq + | A.Neq -> L.build_icmp L.Icmp.Ne | A.Less -> L.build_icmp L.Icmp.Slt | A.Leq -> L.build_icmp L.Icmp.Sle | A.Greater -> L.build_icmp L.Icmp.Sgt diff --git a/src/demo.gal b/src/demo.gal new file mode 100644 index 0000000..13e361a --- /dev/null +++ b/src/demo.gal @@ -0,0 +1,74 @@ +int main(){ + + print_endline(); + + /* Declare our nodes above */ + node n1; + n1 = |"A": 2, "B", 11, "C", 4, "D", 14, "E"|; + node n2; + n2 = |"B": 7, "C", 3, "A", 20, "D"|; + node n3; + n3 = |"C": 5, "D", 5, "A", 16, "E"|; + node n4; + n4 = |"D": 20, "A", 7, "B"|; + + print_line("Lets print them to see what we got:"); + print_elist(n1); + print_elist(n2); + print_elist(n3); + print_elist(n4); + + print_endline(); + print_endline(); + + /* Lets declare another node. But using diffrent syntax */ + elist n5; + n5 = [|"E", 24, "D"|::|"E", 13, "B"|]; + + print_line("We can also print them as a graph:"); + nlist graph; + graph = [n1::n2::n3::n4::n5]; + + /* We can use a different function to print this graph */ + print_nlist(graph); + print_endline(); + + + graph = npop(graph); + print_nlist(graph); + graph = npop(graph); + print_nlist(graph); + graph = npop(graph); + print_nlist(graph); + + + slist testpops; + testpops = ["A"::"B"::"C"]; + print_slist(testpops); + testpops = spop(testpops); + print_slist(testpops); + testpops = spop(testpops); + print_slist(testpops); + + + + + + print_line("Lets get the heaviest edge of the node n1:"); + edge heaviest; + heaviest = get_heaviest_edge(n1); + print_edge(heaviest); + print_endline(); + + print_line("How about the heaviest edge in our graph? Sure:"); + heaviest = get_heaviest_graph_edge(graph); + print_edge(heaviest); + print_endline(); + + print_line("Lets get the node that has the most edges"); + node important; + important = get_most_edges_node(graph); + print_line(source(epeek(important))); + + return 0; +} \ No newline at end of file diff --git a/src/dfs.gal b/src/dfs.gal new file mode 100644 index 0000000..9eb1f92 --- /dev/null +++ b/src/dfs.gal @@ -0,0 +1,159 @@ +int dfs(nlist graph,string A) +{ + + int found; + found =0; + + slist visited; + slist stack; + stack = ["A"]; + + elist v; + int s_counter; + string temp_str; + string node_name; + int node_found; + + nlist temp; + temp = graph; + int graph_length; + graph_length = nlength(graph); + int i; + i = 0; + int count; + count = 0; + string v_dest; + + string v_source; + visited = [""]; + int streq_val; + string top_of_stack; + elist use_node; + elist temp_node; + string temp_source; + string temp_dest; + slist stack_temp; + elist use_node_temp; + + stack_temp = stack; + int count_loop; + count_loop = 0; + string temp_visited; + + while(count<7) + { + if(i >= graph_length) + { + return found; + } + else + { + + } + if(count>0){ + /*print_str(speek(stack_temp));*/ + stack_temp = snext(stack); + stack = snext(stack); + + } + else{ + + } + + top_of_stack = speek(stack_temp); + visited = sadd_back(visited,top_of_stack); + /*this might give us issues*/ + + /*Iterate through graph to find correct edge*/ + i = 0; + temp = graph; + while(i < graph_length) + { + temp_node = npeek(temp); + temp_source = source(epeek(temp_node)); + + streq_val = streq(temp_source,top_of_stack); + if(streq_val == 0) + { + use_node = temp_node; + } + else + { + + } + temp = nnext(temp); + i = i + 1; + } + + /*temp = nnext(temp);*/ + + + + /*v_source = source(epeek(use_node)); + v_dest = dest(epeek(use_node)); + visited = sadd_back(visited,v_source);*/ + + i = 0; + + use_node_temp = use_node; + + while(i [] then diff --git a/src/parser.mly b/src/parser.mly index 9f0ee6d..7e6afa0 100644 --- a/src/parser.mly +++ b/src/parser.mly @@ -12,7 +12,7 @@ %token SEMI LPAREN RPAREN LSQBRACE RSQBRACE LBRACE RBRACE BAR COLON LISTSEP COMMA %token EPLUS EMINUS PLUS MINUS TIMES DIVIDE ASSIGN NOT -%token EQ LT LEQ GT GEQ AND OR +%token EQ LT LEQ GT GEQ AND OR NEQ %token RETURN IF ELSE FOR INT STRING EDGE SLISTT NLISTT ELISTT ILISTT DEFINE WHILE %token LITINT %token ID @@ -23,7 +23,7 @@ %right ASSIGN %left OR %left AND -%left EQ +%left EQ NEQ %left LT GT LEQ GEQ %left PLUS MINUS %left TIMES DIVIDE @@ -112,6 +112,7 @@ expr: | expr TIMES expr { Binop($1, Mult, $3) } | expr DIVIDE expr { Binop($1, Div, $3) } | expr EQ expr { Binop($1, Equal, $3) } + | expr NEQ expr { Binop($1, Neq, $3) } | expr LT expr { Binop($1, Less, $3) } | expr LEQ expr { Binop($1, Leq, $3) } | expr GT expr { Binop($1, Greater, $3) } diff --git a/src/scanner.mll b/src/scanner.mll index 480c691..d606b32 100644 --- a/src/scanner.mll +++ b/src/scanner.mll @@ -26,6 +26,7 @@ rule token = parse | '=' { ASSIGN } | "::" { LISTSEP } | "==" { EQ } +| "!=" { NEQ } | '<' { LT } | "<=" { LEQ } | '>' { GT } diff --git a/src/semant.ml b/src/semant.ml index 160f085..6cdd0bf 100644 --- a/src/semant.ml +++ b/src/semant.ml @@ -67,6 +67,18 @@ let spop_fdcl = { typ = SListtyp; fname = "spop"; formals = [(SListtyp, "a")]; locals = []; body = []};; +let ipop_fdcl = + { typ = IListtyp; fname = "ipop"; formals = [(IListtyp, "a")]; + locals = []; body = []};; + +let epop_fdcl = + { typ = EListtyp; fname = "epop"; formals = [(EListtyp, "a")]; + locals = []; body = []};; + +let npop_fdcl = + { typ = NListtyp; fname = "npop"; formals = [(NListtyp, "a")]; + locals = []; body = []};; + let speek_fdcl = { typ = String; fname = "speek"; formals = [(SListtyp, "a")]; locals = []; body = []};; @@ -116,7 +128,7 @@ let nadd_fdcl = locals = []; body = []};; let str_comp_fdcl = - { typ = Int; fname = "str_comp"; formals = [(String, "a"); (String, "b")]; + { typ = Int; fname = "streq"; formals = [(String, "a"); (String, "b")]; locals = []; body = []};; @@ -126,7 +138,7 @@ let builtin_fdcl_list = speek_fdcl; ipeek_fdcl; epeek_fdcl; snext_fdcl; elength_fdcl; enext_fdcl; inext_fdcl; ilength_fdcl; nnext_fdcl; npeek_fdcl; nlength_fdcl; sadd_fdcl; eadd_fdcl; iadd_fdcl; nadd_fdcl; - str_comp_fdcl ];; + str_comp_fdcl; ipop_fdcl; epop_fdcl; npop_fdcl ];; (* Static semantic checker of the program. Will return void @@ -241,7 +253,7 @@ let check_func exp_list globs_map func_decl funcs_map = in (match op with (* Integer operators *) | Add | Sub | Mult | Div | Equal | Less | Leq - | Greater | Geq | And | Or + | Greater | Geq | And | Or | Neq when (v1 = Int && v2 = Int) -> (Int, exp_list) (* List operators *) (* | Eadd | Esub when v1 = Listtyp && v2 = Listtyp -> (Listtyp, exp_list) *) @@ -509,14 +521,3 @@ let check (globals, funcs) = (extract_locals [] funcs) *) ;; - - - - - - - - - - - diff --git a/src/stdlib_code.gal b/src/stdlib_code.gal index a818de1..5070b42 100644 --- a/src/stdlib_code.gal +++ b/src/stdlib_code.gal @@ -1,3 +1,131 @@ +elist get_most_edges_node(nlist graph){ + + int len; + len = nlength(graph); + int i; + i = 0; + + ilist lengths; + lengths = []; + + nlist temp; + temp = graph; + + /* Get the number of edges */ + while( (i < len) && (len > 0)){ + lengths = iadd(elength(npeek(temp)), lengths); + temp = nnext(temp); + i = i + 1; + } + lengths = irev(lengths); + + len = ilength(lengths); + i = 0; + int longest; + longest = 0; + int order; + order = 1; + + while( (i < len) && (len > 0)){ + if(longest < ipeek(lengths)){ + longest = ipeek(lengths); + order = i + 1; + }else{} + lengths = inext(lengths); + i = i + 1; + } + + temp = graph; + elist result; + result = []; + + while(order > 1){ + temp = nnext(temp); + } + + result = npeek(temp); + return result; +} + +edge get_heaviest_graph_edge(nlist l1){ + + int len; + len = nlength(l1); + int i; + i = 0; + int heaviest_w; + heaviest_w = 0; + + edge heaviest; + heaviest = |"EMPTY", 0, "EMPTY"|; + + elist temp; + temp = []; + + while( (i < len) && (len > 0) ){ + + /* Get the head of the list and move forward */ + temp = npeek(l1); + l1 = nnext(l1); + + /* Get the weight of the element */ + if( heaviest_w < weight(get_heaviest_edge(temp)) ){ + heaviest_w = weight(get_heaviest_edge(temp)); + heaviest = get_heaviest_edge(temp); + }else{} + + /* Increment */ + i = i + 1; + } + + return heaviest; +} + +/* Function will return the ehaviest edge of the node */ +edge get_heaviest_edge(node n1){ + + int len; + len = elength(n1); + int i; + i = 0; + + int heaviest_w; + heaviest_w = 0; + + edge heaviest; + heaviest = |"EMPTY", 0, "EMPTY"|; + + edge temp; + temp = |"", 0, ""|; + + /* Iterate through the list, compare weights of edges */ + while( (i < len) && (len > 0) ){ + + /* Get the head of the list and move forward */ + temp = epeek(n1); + n1 = enext(n1); + + /* Get the weight of the element */ + if( heaviest_w < weight(temp) ){ + heaviest_w = weight(temp); + heaviest = temp; + }else{} + + /* Increment */ + i = i + 1; + } + + return heaviest; +} + + +int print_line(string str){ + print_str(str); + print_endline(); + return 0; +} + + int print_sl_len(slist lister){ print_int(slength(lister)); print_endline(); @@ -303,22 +431,23 @@ nlist nconcat(nlist l1, nlist l2){ return l1; } +/* int main(){ - - nlist l1; - nlist l2; + node n1; - node n2; - n1 = |"A":2,"B"|; - n2 = |"C":3,"D"|; - l1 = [n1]; - l2 = [n2]; - nlist l3; - l3 = nconcat(l1,l2); - print_nlist(l3); - + n1 = |"A":2,"B",3,"C"|; + edge e2; + edge e3; + e2 = |"A",2,"B"|; + e3 = |"A",2,"B"|; - /* + int i; + i = streq("A","A"); + + n1 = eadd(|"H",10,"L"|,n1); + print_elist(n1); + + node n1; n1 = |"A": 2, "B", 11, "C", 2, "D"|; nlist n2; @@ -402,19 +531,19 @@ int main(){ print_slist(l5); print_sl_len(l5); - /*string can; + string can; string orange; can = "panda"; orange = "batman"; int comparison; - comparison = str_comp(can, orange); + comparison = str_comp(can, can); print_int(comparison); - print_endline(); */ + print_endline(); return 1; } - +*/ diff --git a/testall.sh b/src/testall.sh similarity index 97% rename from testall.sh rename to src/testall.sh index bee58bf..c5be9fd 100755 --- a/testall.sh +++ b/src/testall.sh @@ -6,12 +6,12 @@ # Compile and check the error of each expected-to-fail test # Path to the LLVM interpreter -#LLI="lli" -LLI="/usr/local/opt/llvm/bin/lli" +LLI="lli" +#LLI="/usr/local/opt/llvm/bin/lli" # Path to the microc compiler. Usually "./microc.native" # Try "_build/microc.native" if ocamlbuild was unable to create a symbolic link. -GAL="./src/gal.native" +GAL="./gal.native" #GAL="_build/microc.native" # Set time limit for all operations @@ -162,7 +162,7 @@ if [ $# -ge 1 ] then files=$@ else - files="tests/test_*.gal tests/fail_*.gal" + files="../tests/test_*.gal ../tests/fail_*.gal" fi for file in $files diff --git a/stdlib/list.gal b/stdlib/list.gal deleted file mode 100644 index 8b13789..0000000 --- a/stdlib/list.gal +++ /dev/null @@ -1 +0,0 @@ - diff --git a/syntax_do_not_delete.txt b/syntax_do_not_delete.txt deleted file mode 100644 index c5d91bd..0000000 --- a/syntax_do_not_delete.txt +++ /dev/null @@ -1,5 +0,0 @@ -edge e1 = |"A", 2, "B"| - -/* The next two are equivalent*/ -node n1 = [|"A": 2,"D",3,"E"|] <-- ignore this one until further notice. -node n1 = [|"A": 2, "D"| :: |"A",3,"E"|] \ No newline at end of file diff --git a/tests/fail_redefine_existing_function.err b/tests/fail_redefine_existing_function.err index 38e9448..a928df6 100644 --- a/tests/fail_redefine_existing_function.err +++ b/tests/fail_redefine_existing_function.err @@ -1,2 +1,5 @@ Fatal error: exception Failure(" - cannot redefine print_int") \ No newline at end of file + in print_edge fcall: print_int expects 0 arguments + in print_ilist fcall: print_int expects 0 arguments + cannot redefine print_int + in print_sl_len fcall: print_int expects 0 arguments ") \ No newline at end of file diff --git a/tests/fail_redefine_existing_function.gal b/tests/fail_redefine_existing_function.gal index 20c4ddb..ab3a81e 100644 --- a/tests/fail_redefine_existing_function.gal +++ b/tests/fail_redefine_existing_function.gal @@ -1,4 +1,7 @@ -int print_int() {} +int print_int() +{ + +} int main() { diff --git a/tests/stdlib_code.gal b/tests/stdlib_code.gal new file mode 100644 index 0000000..6bcef95 --- /dev/null +++ b/tests/stdlib_code.gal @@ -0,0 +1,548 @@ +elist get_most_edges_node(nlist graph){ + + int len; + len = nlength(graph); + int i; + i = 0; + + ilist lengths; + lengths = []; + + nlist temp; + temp = graph; + + /* Get the number of edges */ + while( (i < len) && (len > 0)){ + lengths = iadd(elength(npeek(temp)), lengths); + temp = nnext(temp); + i = i + 1; + } + lengths = irev(lengths); + + len = ilength(lengths); + i = 0; + int longest; + longest = 0; + int order; + order = 1; + + while( (i < len) && (len > 0)){ + if(longest < ipeek(lengths)){ + longest = ipeek(lengths); + order = i + 1; + }else{} + lengths = inext(lengths); + i = i + 1; + } + + temp = graph; + elist result; + result = []; + + while(order > 1){ + temp = nnext(temp); + } + + result = npeek(temp); + return result; +} + +edge get_heaviest_graph_edge(nlist l1){ + + int len; + len = nlength(l1); + int i; + i = 0; + int heaviest_w; + heaviest_w = 0; + + edge heaviest; + heaviest = |"EMPTY", 0, "EMPTY"|; + + elist temp; + temp = []; + + while( (i < len) && (len > 0) ){ + + /* Get the head of the list and move forward */ + temp = npeek(l1); + l1 = nnext(l1); + + /* Get the weight of the element */ + if( heaviest_w < weight(get_heaviest_edge(temp)) ){ + heaviest_w = weight(get_heaviest_edge(temp)); + heaviest = get_heaviest_edge(temp); + }else{} + + /* Increment */ + i = i + 1; + } + + return heaviest; +} + +/* Function will return the ehaviest edge of the node */ +edge get_heaviest_edge(node n1){ + + int len; + len = elength(n1); + int i; + i = 0; + + int heaviest_w; + heaviest_w = 0; + + edge heaviest; + heaviest = |"EMPTY", 0, "EMPTY"|; + + edge temp; + temp = |"", 0, ""|; + + /* Iterate through the list, compare weights of edges */ + while( (i < len) && (len > 0) ){ + + /* Get the head of the list and move forward */ + temp = epeek(n1); + n1 = enext(n1); + + /* Get the weight of the element */ + if( heaviest_w < weight(temp) ){ + heaviest_w = weight(temp); + heaviest = temp; + }else{} + + /* Increment */ + i = i + 1; + } + + return heaviest; +} + + +int print_line(string str){ + print_str(str); + print_endline(); + return 0; +} + + +int print_sl_len(slist lister){ + print_int(slength(lister)); + print_endline(); + return 0; +} + +int print_slist(slist l1){ + int len; + len = slength(l1); + slist tmp; + tmp = l1; + int i; + i = 0; + print_str("->"); + while (i < len) { + print_str(speek(tmp)); + print_str("::"); + tmp = snext(tmp); + i = i + 1; + } + print_endline(); + + return 1; +} + +int print_edge(edge e){ + print_str("|"); + print_str(source(e)); + print_str(", "); + print_int(weight(e)); + print_str(", "); + print_str(dest(e)); + print_str("|"); + return 0; +} + +int print_elist(elist l1){ + int len; + len = elength(l1); + elist tmp; + tmp = l1; + int i; + i = 0; + print_str("->"); + while (i < len) { + print_edge(epeek(tmp)); + print_str("::"); + tmp = enext(tmp); + i = i + 1; + } + print_endline(); + + return 1; +} + +int print_ilist(ilist l1){ + int len; + len = ilength(l1); + ilist tmp; + tmp = l1; + int i; + i = 0; + print_str("->"); + while (i < len) { + print_int(ipeek(tmp)); + print_str("::"); + tmp = inext(tmp); + i = i + 1; + } + print_endline(); + + return 1; +} + +int print_nlist(nlist l1){ + int len; + len = nlength(l1); + nlist tmp; + tmp = l1; + int i; + i = 0; + print_str("->"); + while (i < len) { + print_elist(npeek(tmp)); + print_str("::"); + tmp = nnext(tmp); + i = i + 1; + } + print_endline(); + + return 1; +} + +ilist irev(ilist l1){ + + int len_l1; + len_l1 = ilength(l1); + ilist temp_l1; + temp_l1 = []; + int temp_element; + + while(!(len_l1 ==0)){ + + /*adds the first element of the list l1 to temp_l1*/ + temp_element = ipeek(l1); + temp_l1 = iadd(temp_element,temp_l1); + + /*advances the head of the list*/ + l1 = inext(l1); + + len_l1 = len_l1 - 1; + + } + return temp_l1; +} + +slist srev(slist l1){ + + int len_l1; + len_l1 = slength(l1); + slist temp_l1; + temp_l1 = []; + string temp_element; + + while(!(len_l1 ==0)){ + + /*adds the first element of the list l1 to temp_l1*/ + temp_element = speek(l1); + temp_l1 = sadd(temp_element,temp_l1); + + /*advances the head of the list*/ + l1 = snext(l1); + + len_l1 = len_l1 - 1; + + } + return temp_l1; +} + +elist erev(elist l1){ + + int len_l1; + len_l1 = elength(l1); + elist temp_l1; + temp_l1 = []; + edge temp_element; + + while(!(len_l1 ==0)){ + + /*adds the first element of the list l1 to temp_l1*/ + temp_element = epeek(l1); + temp_l1 = eadd(temp_element,temp_l1); + + /*advances the head of the list*/ + l1 = enext(l1); + + len_l1 = len_l1 - 1; + + } + return temp_l1; +} + +nlist nrev(nlist l1){ + + int len_l1; + len_l1 = nlength(l1); + nlist temp_l1; + temp_l1 = []; + node temp_element; + + while(!(len_l1 ==0)){ + + /*adds the first element of the list l1 to temp_l1*/ + temp_element = npeek(l1); + temp_l1 = nadd(temp_element,temp_l1); + + /*advances the head of the list*/ + l1 = nnext(l1); + + len_l1 = len_l1 - 1; + + } + return temp_l1; +} + + +ilist iadd_back(ilist l1,int i){ + + + l1 = irev(l1); + l1 = iadd(i,l1); + l1 = irev(l1); + return l1; + +} + +slist sadd_back(slist l1,string i){ + + + l1 = srev(l1); + l1 = sadd(i,l1); + l1 = srev(l1); + return l1; + +} + +elist eadd_back(elist l1,edge i){ + + + l1 = erev(l1); + l1 = eadd(i,l1); + l1 = erev(l1); + return l1; + +} + +nlist nadd_back(nlist l1,node i){ + + + l1 = nrev(l1); + l1 = nadd(i,l1); + l1 = nrev(l1); + return l1; +} + +ilist iconcat(ilist l1, ilist l2){ + + l1 = irev(l1); + int len_l2; + len_l2 = ilength(l2); + int temp_element; + + while(!(len_l2==0)){ + + temp_element = ipeek(l2); + l1 = iadd(temp_element,l1); + l2 = inext(l2); + + len_l2 = len_l2 - 1; + } + + l1 = irev(l1); + return l1; +} + +slist sconcat(slist l1, slist l2){ + + l1 = srev(l1); + int len_l2; + len_l2 = slength(l2); + string temp_element; + + while(!(len_l2==0)){ + + temp_element = speek(l2); + l1 = sadd(temp_element,l1); + l2 = snext(l2); + + len_l2 = len_l2 - 1; + } + + l1 = srev(l1); + return l1; +} + +elist econcat(elist l1, elist l2){ + + l1 = erev(l1); + int len_l2; + len_l2 = elength(l2); + edge temp_element; + + while(!(len_l2==0)){ + + temp_element = epeek(l2); + l1 = eadd(temp_element,l1); + l2 = enext(l2); + + len_l2 = len_l2 - 1; + } + + l1 = erev(l1); + return l1; +} + +nlist nconcat(nlist l1, nlist l2){ + + l1 = nrev(l1); + int len_l2; + len_l2 = nlength(l2); + node temp_element; + + while(!(len_l2==0)){ + + temp_element = npeek(l2); + l1 = nadd(temp_element,l1); + l2 = nnext(l2); + + len_l2 = len_l2 - 1; + } + + l1 = nrev(l1); + return l1; +} + +/* +int main(){ + + nlist l1; + nlist l2; + node n1; + node n2; + n1 = |"A":2,"B"|; + n2 = |"C":3,"D"|; + l1 = [n1]; + l2 = [n2]; + nlist l3; + l3 = nconcat(l1,l2); + print_nlist(l3); + + + node n1; + n1 = |"A": 2, "B", 11, "C", 2, "D"|; + nlist n2; + n2 = [n1]; + print_nlist(n2); + nlist n3; + n3 = nadd_back(n2,|"D":3,"HELLO"|); + print_nlist(n3); + + + slist l1; + l1 = ["A"::"B"::"C"]; + print_slist(l1); + slist l20; + l20 = srev(l1); + print_slist(l20); + + + string s1; + s1 = "Hello"; + l1 = sadd(s1, l1); + print_slist(l1); + print_sl_len(l1); + l1 = spop(l1); + print_slist(l1); + print_sl_len(l1); + + + node n1; + n1 = |"A": 2, "B", 11, "C", 2, "D"|; + + + elist l2; + l2 = n1; + print_elist(l2); + elist l30; + l30 = erev(l2); + print_elist(l30); + + + + ilist l3; + l3 = [1::2::3::4::42]; + print_ilist(l3); + ilist l10; + l10 = irev(l3); + print_ilist(l10); + + l3 = iadd(27, l3); + print_ilist(l3); + print_int(ilength(l3)); + print_endline(); + + + + node n2; + n2 = |"B": 7, "A", 4, "D"|; + node n1; + n1 = |"A": 2, "B", 11, "C", 2, "D"|; + + nlist l4; + l4 = [n1::n2]; + print_nlist(l4); + nlist l40; + l40 = nrev(l4); + print_nlist(l40); + + + + slist l5; + l5 = []; + + int banana; + banana = slength(l5); + print_int(banana); + print_endline(); + + + l5 = sadd("hello", l5); + + print_slist(l5); + print_sl_len(l5); + + string can; + string orange; + can = "panda"; + orange = "batman"; + + int comparison; + comparison = str_comp(can, can); + print_int(comparison); + print_endline(); + + + return 1; +} +*/ + + diff --git a/tests/test_compare_strings1.gal b/tests/test_compare_strings1.gal new file mode 100644 index 0000000..5341620 --- /dev/null +++ b/tests/test_compare_strings1.gal @@ -0,0 +1,17 @@ +int main() +{ + + string a; + string b; + int bool; + + a = "A"; + b = "B"; + + bool = streq(a,b); + + print_int(bool); + + return 1; + +} \ No newline at end of file diff --git a/tests/test_compare_strings1.out b/tests/test_compare_strings1.out new file mode 100644 index 0000000..3a2e3f4 --- /dev/null +++ b/tests/test_compare_strings1.out @@ -0,0 +1 @@ +-1 diff --git a/tests/test_get_heaviest_graph_edge.gal b/tests/test_get_heaviest_graph_edge.gal new file mode 100644 index 0000000..7bf77d7 --- /dev/null +++ b/tests/test_get_heaviest_graph_edge.gal @@ -0,0 +1,35 @@ +int main() +{ + /* + A - 1 + B - 2 + C - 3 + D - 4 + E - 5 + F - 6 + */ + + node n1; + n1 = |"A": 7, "B", 14, "F", 9, "C"|; + node n2; + n2 = |"B": 7, "A", 10, "C"|; + node n3; + n3 = |"C": 5, "D", 16, "E"|; + node n4; + n4 = |"D": 20, "A", 7, "B"|; + + elist n5; + n5 = [|"E", 24, "D"|::|"E", 13, "B"|]; + + nlist graph; + graph = [n1::n2::n3::n4::n5]; + + edge temp; + + temp = get_heaviest_graph_edge(graph); + + print_edge(temp); + + return 1; + +} \ No newline at end of file diff --git a/tests/test_get_heaviest_graph_edge.out b/tests/test_get_heaviest_graph_edge.out new file mode 100644 index 0000000..f62bcc0 --- /dev/null +++ b/tests/test_get_heaviest_graph_edge.out @@ -0,0 +1 @@ +|E, 24, D| \ No newline at end of file diff --git a/tests/test_get_most_edges_node.gal b/tests/test_get_most_edges_node.gal new file mode 100644 index 0000000..a335d1f --- /dev/null +++ b/tests/test_get_most_edges_node.gal @@ -0,0 +1,35 @@ +int main() +{ + /* + A - 1 + B - 2 + C - 3 + D - 4 + E - 5 + F - 6 + */ + + node n1; + n1 = |"A": 7, "B", 14, "F", 9, "C"|; + node n2; + n2 = |"B": 7, "A", 10, "C"|; + node n3; + n3 = |"C": 5, "D", 16, "E"|; + node n4; + n4 = |"D": 20, "A", 7, "B"|; + + elist n5; + n5 = [|"E", 24, "D"|::|"E", 13, "B"|]; + + nlist graph; + graph = [n1::n2::n3::n4::n5]; + + elist temp; + + temp = get_most_edges_node(graph); + + print_elist(temp); + + return 1; + +} \ No newline at end of file diff --git a/tests/test_get_most_edges_node.out b/tests/test_get_most_edges_node.out new file mode 100644 index 0000000..29390fa --- /dev/null +++ b/tests/test_get_most_edges_node.out @@ -0,0 +1 @@ +->|A, 7, B|::|A, 14, F|::|A, 9, C|:: \ No newline at end of file diff --git a/tests/test_print_ilist.gal b/tests/test_print_ilist.gal new file mode 100644 index 0000000..c62fcb9 --- /dev/null +++ b/tests/test_print_ilist.gal @@ -0,0 +1,13 @@ +int main() +{ + + ilist x; + + x = [1]; + x = iadd(2,x); + x = iadd(100,x); + + + print_ilist(x); + return 1; +} diff --git a/tests/test_print_ilist.out b/tests/test_print_ilist.out new file mode 100644 index 0000000..0106c06 --- /dev/null +++ b/tests/test_print_ilist.out @@ -0,0 +1 @@ +->100::2::1:: \ No newline at end of file diff --git a/tests/test_print_ilist2.gal b/tests/test_print_ilist2.gal new file mode 100644 index 0000000..b4ecd9b --- /dev/null +++ b/tests/test_print_ilist2.gal @@ -0,0 +1,11 @@ +int main() +{ + ilist l1; + + l1 = [0]; + + l1 = iadd_back(l1,1); + + print_ilist(l1); + +} \ No newline at end of file diff --git a/tests/test_print_ilist2.out b/tests/test_print_ilist2.out new file mode 100644 index 0000000..49e45a9 --- /dev/null +++ b/tests/test_print_ilist2.out @@ -0,0 +1 @@ +->0::1:: \ No newline at end of file diff --git a/tests/test_print_ilist_rev.gal b/tests/test_print_ilist_rev.gal new file mode 100644 index 0000000..8280fd2 --- /dev/null +++ b/tests/test_print_ilist_rev.gal @@ -0,0 +1,20 @@ +int main() +{ + + ilist x; + ilist rev_x; + + x = [11]; + x = iadd(10,x); + x = iadd(9,x); + x = iadd(8,x); + x = iadd(7,x); + x = iadd(1,x); + x = iadd(2,x); + x = iadd(3,x); + + x = irev(x); + + print_ilist(x); + return 1; +} \ No newline at end of file diff --git a/tests/test_print_ilist_rev.out b/tests/test_print_ilist_rev.out new file mode 100644 index 0000000..1f84b2a --- /dev/null +++ b/tests/test_print_ilist_rev.out @@ -0,0 +1 @@ +->11::10::9::8::7::1::2::3:: \ No newline at end of file diff --git a/tests/test_print_int1.gal b/tests/test_print_int1.gal new file mode 100644 index 0000000..b375a70 --- /dev/null +++ b/tests/test_print_int1.gal @@ -0,0 +1,8 @@ +int main(){ + + int a; + a = 5; + + print_int(a); + return 1; +} \ No newline at end of file diff --git a/tests/test_print_int1.out b/tests/test_print_int1.out new file mode 100644 index 0000000..7813681 --- /dev/null +++ b/tests/test_print_int1.out @@ -0,0 +1 @@ +5 \ No newline at end of file