def test(): text = """ class A inherits B { }; class B inherits A { f ( ) : Int { g() } ; } ; """ print("corriendo") ast = run_pipeline(text) errors = [] collector = TypeCollector(errors) collector.visit(ast) context = collector.context builder = TypeBuilder(context, errors) builder.visit(ast) checker = TypeChecker(context, errors) checker.visit(ast, None) if errors != [ "A is involved in a cyclic heritage", "A class Main with a method main most be provided", 'Method "g" is not defined in B.', ]: print(errors) assert False assert True
def test(): text = """ class B inherits A { a : String ; b : AUTO_TYPE ; }; class A inherits B { } ; """ print("corriendo") ast = run_pipeline(text) errors = [] collector = TypeCollector(errors) collector.visit(ast) context = collector.context builder = TypeBuilder(context, errors) builder.visit(ast) checker = TypeChecker(context, errors) checker.visit(ast, None) print(errors) assert errors == [ "B is involved in a cyclic heritage", "A class Main with a method main most be provided", ]
def test(): text = """ class A { a : String ; } ; class Point inherits A { f ( a : AUTO_TYPE , b : AUTO_TYPE ) : AUTO_TYPE { if ( a = 1 ) then b else g ( a + 1 , b / 2 ) fi } ; g ( a : AUTO_TYPE , b : AUTO_TYPE ) : AUTO_TYPE { if ( b = 1 ) then a else f ( a / 2 , b + 1 ) fi } ; } ; """ ast = run_pipeline(text) errors = [] collector = TypeCollector(errors) collector.visit(ast) context = collector.context builder = TypeBuilder(context, errors) builder.visit(ast) checker = TypeChecker(context, errors) __ = checker.visit(ast, None) tset_builder = TSetBuilder(context, errors) tset = tset_builder.visit(ast, None) tset_reducer = TSetReducer(context, errors) reduced_set = tset_reducer.visit(ast, tset) print("Errors:", errors) print("Context:") print(context) print(reduced_set) assert errors == ["A class Main with a method main most be provided"]
def test(): text = """ class B inherits Point { x : String ; init ( n : String , z : Int , z : Int) : Bool { { n <- "ok" ; z <- 1 + 3 ; } } ; } ; class Point { x : AUTO_TYPE ; y : AUTO_TYPE ; init ( n : Int , m : Int ) : Int { { x <- n ; y <- m ; } } ; } ; """ print("corriendo") ast = run_pipeline(text) errors = [] collector = TypeCollector(errors) collector.visit(ast) context = collector.context builder = TypeBuilder(context, errors) builder.visit(ast) checker = TypeChecker(context, errors) checker.visit(ast, None) assert errors == [ 'Attribute "x" is already defined in B.', "A class Main with a method main most be provided", "More tan one param in method init has the name z", 'Cannot convert "Int" into "Bool".', 'Method "init" already defined in "an ancestor of B" with a different signature.', ]
def test(): text = """ class A { a : String ; } ; class Point inherits A { ackermann ( m : AUTO_TYPE , n : AUTO_TYPE ) : AUTO_TYPE { if m < 0 then 1 else if n < 0 then ackermann ( m - 1 , 1 ) else ackermann ( m - 1 , ackermann ( m , n - 1 ) ) fi fi } ; } ; """ ast = run_pipeline(text) errors = [] collector = TypeCollector(errors) collector.visit(ast) context = collector.context builder = TypeBuilder(context, errors) builder.visit(ast) checker = TypeChecker(context, errors) __ = checker.visit(ast, None) tset_builder = TSetBuilder(context, errors) tset = tset_builder.visit(ast, None) tset_reducer = TSetReducer(context, errors) reduced_set = tset_reducer.visit(ast, tset) print("Errors:", errors) print("Context:") print(context) print(reduced_set) assert errors == ["A class Main with a method main most be provided"]
def test(): text = """ class Main inherits Point { x : String ; init ( n : String , m : Int) : Int { { n <- "Testing main" ; m <- 1 + 2 ; } } ; main ( n : String , m : Int ) : Int { { n <- "Buen dia" ; m <- 3 ; } } ; } ; class Point { x : AUTO_TYPE ; y : AUTO_TYPE ; init ( n : String , m : Int ) : Int { { x <- n ; y <- m ; } } ; } ; """ ast = run_pipeline(text) errors = [] collector = TypeCollector(errors) collector.visit(ast) context = collector.context builder = TypeBuilder(context, errors) builder.visit(ast) checker = TypeChecker(context, errors) checker.visit(ast, None) assert errors == [ 'Attribute "x" is already defined in Main.', '"main" method in class Main does not receive any parameters', ]
def test(): text = """ class Main inherits Point { x : String ; init ( n : String , m : Int) : Int { { n <- "Testing main" ; m <- 1 + 2 ; } } ; } ; class Point { x : AUTO_TYPE ; y : AUTO_TYPE ; init ( n : String , m : Int ) : Int { { x <- n ; y <- m ; } } ; main ( n : String , m : Int ) : Int { { x <- n ; y <- m ; } } ; } ; """ ast = run_pipeline(text) errors = [] collector = TypeCollector(errors) collector.visit(ast) context = collector.context builder = TypeBuilder(context, errors) builder.visit(ast) checker = TypeChecker(context, errors) checker.visit(ast, None) assert errors == [ 'Attribute "x" is already defined in Main.', "A class Main with a method main most be provided", ]
def test(): text = """ class C inherits J { } ; class A inherits B { } ; class B inherits A { } ; class C { } ; class D inherits E { } ; class E inherits F { } ; class F inherits D { } ; class G inherits F { } ; """ ast = run_pipeline(text) errors = [] collector = TypeCollector(errors) collector.visit(ast) context = collector.context builder = TypeBuilder(context, errors) builder.visit(ast) err = [ "Type with the same name (C) already in context.", 'Type "J" is not defined.', "Parent type is already set for C.", "A is involved in a cyclic heritage", "D is involved in a cyclic heritage", "A class Main with a method main most be provided", ] checker = TypeChecker(context, errors) checker.visit(ast, None) if errors != err: print(errors) assert False assert True
def test(): text = """ (* This program prints the first 10 numbers of fibonacci *) class Main { main(): Object { let total: AUTO_TYPE <- 10, i: AUTO_TYPE <- 1 , io: AUTO_TYPE <- new IO in while i <= total loop { io.out_int(fibonacci(i)); io.out_string("n"); i <- i + 1; } pool }; fibonacci (n: AUTO_TYPE): AUTO_TYPE { if n <= 2 then 1 else fibonacci(n - 1) + fibonacci(n - 2) fi }; }; """ ast = run_pipeline(text) errors = [] collector = TypeCollector(errors) collector.visit(ast) context = collector.context builder = TypeBuilder(context, errors) builder.visit(ast) checker = TypeChecker(context, errors) checker.visit(ast, None) print(errors) if errors != []: print(errors) assert False tset_builder = TSetBuilder(context, errors) tset = tset_builder.visit(ast, None) tset_reducer = TSetReducer(context, errors) reduced_set = tset_reducer.visit(ast, tset) tset_merger = TSetMerger(context, errors) tset_merger.visit(ast, reduced_set) collector = TypeCollector(errors) collector.visit(ast) context = collector.context builder = TypeBuilder(context, errors) builder.visit(ast) checker = TypeChecker(context, errors) checker.visit(ast, None) formatter = FormatVisitor() tree = formatter.visit(ast) print("Errors:", errors) print("Context:") print(context) print(reduced_set) print(tree) assert errors == []
def test(): text = """ class Main { main (): Object { 0 }; f (a: AUTO_TYPE, b: AUTO_TYPE, c: AUTO_TYPE, d: AUTO_TYPE): AUTO_TYPE { { a <- b; b <- c; c <- d; d <- a; d + 1; a; } }; }; """ ast = run_pipeline(text) errors = [] collector = TypeCollector(errors) collector.visit(ast) context = collector.context builder = TypeBuilder(context, errors) builder.visit(ast) checker = TypeChecker(context, errors) checker.visit(ast, None) print(errors) if errors != []: print(errors) assert False tset_builder = TSetBuilder(context, errors) tset = tset_builder.visit(ast, None) tset_reducer = TSetReducer(context, errors) reduced_set = tset_reducer.visit(ast, tset) tset_merger = TSetMerger(context, errors) tset_merger.visit(ast, reduced_set) collector = TypeCollector(errors) collector.visit(ast) context = collector.context builder = TypeBuilder(context, errors) builder.visit(ast) checker = TypeChecker(context, errors) checker.visit(ast, None) formatter = FormatVisitor() tree = formatter.visit(ast) print("Errors:", errors) print("Context:") print(context) print(reduced_set) print(tree) assert errors == []
def test(): text = """ class Main inherits IO { main(): IO { let vector: AUTO_TYPE <- (new Vector2).init(0, 0) in vector.print_vector() }; }; class Vector2 { x: AUTO_TYPE; y: AUTO_TYPE; init(x_: AUTO_TYPE, y_: AUTO_TYPE): AUTO_TYPE { { x <- x_; y <- y_; self; } }; get_x(): AUTO_TYPE { x }; get_y(): AUTO_TYPE { y }; add(v: Vector2): AUTO_TYPE { (new Vector2).init(x + v.get_x(), y + v.get_y()) }; print_vector(): AUTO_TYPE { let io: IO <- new IO in { io.out_string(" ( "); io.out_int(get_x()); io.out_string("; "); io.out_int(get_y()); io.out_string(" ) "); } }; clone_vector(): AUTO_TYPE { (new Vector2).init(x, y) }; }; """ ast = run_pipeline(text) errors = [] collector = TypeCollector(errors) collector.visit(ast) context = collector.context builder = TypeBuilder(context, errors) builder.visit(ast) checker = TypeChecker(context, errors) checker.visit(ast, None) print(errors) if errors != []: print(errors) assert False tset_builder = TSetBuilder(context, errors) tset = tset_builder.visit(ast, None) tset_reducer = TSetReducer(context, errors) reduced_set = tset_reducer.visit(ast, tset) tset_merger = TSetMerger(context, errors) tset_merger.visit(ast, reduced_set) collector = TypeCollector(errors) collector.visit(ast) context = collector.context builder = TypeBuilder(context, errors) builder.visit(ast) checker = TypeChecker(context, errors) checker.visit(ast, None) formatter = FormatVisitor() tree = formatter.visit(ast) print("Errors:", errors) print("Context:") print(context) print(reduced_set) print(tree) assert errors == []
def test(): text = """ class Main { main(a : AUTO_TYPE) : AUTO_TYPE { let x : AUTO_TYPE <- 3 in case x of y : IO => y.out_string(a); esac }; }; """ ast = run_pipeline(text) errors = [] collector = TypeCollector(errors) collector.visit(ast) context = collector.context builder = TypeBuilder(context, errors) builder.visit(ast) checker = TypeChecker(context, errors) checker.visit(ast, None) if errors != [ '"main" method in class Main does not receive any parameters', ]: print(errors) assert False tset_builder = TSetBuilder(context, errors) tset = tset_builder.visit(ast, None) tset_reducer = TSetReducer(context, errors) reduced_set = tset_reducer.visit(ast, tset) tset_merger = TSetMerger(context, errors) tset_merger.visit(ast, reduced_set) collector = TypeCollector(errors) collector.visit(ast) context = collector.context builder = TypeBuilder(context, errors) builder.visit(ast) checker = TypeChecker(context, errors) checker.visit(ast, None) formatter = FormatVisitor() tree = formatter.visit(ast) print("Errors:", errors) print("Context:") print(context) print(reduced_set) print(tree) assert errors == [ '"main" method in class Main does not receive any parameters', '"main" method in class Main does not receive any parameters', ]
def test(): text = """ class A { a : String ; b : AUTO_TYPE ; c : AUTO_TYPE <- 0 ; d : Object <- while c loop c + 1 pool ; j : AUTO_TYPE ; l : AUTO_TYPE ; fact ( n : AUTO_TYPE ) : AUTO_TYPE { if n < 0 then 1 else n + fact ( n - 1 ) fi } ; step ( p : AUTO_TYPE ) : AUTO_TYPE { b <- { p + 5 ; j <- p ; p <- false ; isvoid d ; l @ Point . main ( ) ; } } ; } ; class Point inherits A { h : AUTO_TYPE <- "debe ser tipo string" ; k : AUTO_TYPE ; main ( ) : AUTO_TYPE { let i : AUTO_TYPE <- new A in { isvoid i ; (*Puede lanzar error semantico*) } } ; ackermann ( m : AUTO_TYPE , n : AUTO_TYPE ) : AUTO_TYPE { if m < 0 then 1 else if n < 0 then ackermann ( m - 1 , 1 ) else ackermann ( m - 1 , ackermann ( m , n - 1 ) ) fi fi } ; } ; """ ast = run_pipeline(text) errors = [] collector = TypeCollector(errors) collector.visit(ast) context = collector.context builder = TypeBuilder(context, errors) builder.visit(ast) checker = TypeChecker(context, errors) __ = checker.visit(ast, None) tset_builder = TSetBuilder(context, errors) tset = tset_builder.visit(ast, None) tset_reducer = TSetReducer(context, errors) reduced_set = tset_reducer.visit(ast, tset) print("Errors:", errors) print("Context:") print(context) print(reduced_set) assert errors == ["A class Main with a method main most be provided"]
def test(): text = """ class Main { main (): Object { 0 }; f(a: AUTO_TYPE, b: AUTO_TYPE): AUTO_TYPE { if a = 1 then b else g(a + 1, b / 1) fi }; g(a: AUTO_TYPE, b: AUTO_TYPE): AUTO_TYPE { if b = 1 then a else f(a / 2, b + 1) fi }; }; """ ast = run_pipeline(text) errors = [] collector = TypeCollector(errors) collector.visit(ast) context = collector.context builder = TypeBuilder(context, errors) builder.visit(ast) checker = TypeChecker(context, errors) checker.visit(ast, None) print(errors) if errors != []: print(errors) assert False tset_builder = TSetBuilder(context, errors) tset = tset_builder.visit(ast, None) tset_reducer = TSetReducer(context, errors) reduced_set = tset_reducer.visit(ast, tset) tset_merger = TSetMerger(context, errors) tset_merger.visit(ast, reduced_set) collector = TypeCollector(errors) collector.visit(ast) context = collector.context builder = TypeBuilder(context, errors) builder.visit(ast) checker = TypeChecker(context, errors) checker.visit(ast, None) formatter = FormatVisitor() tree = formatter.visit(ast) print("Errors:", errors) print("Context:") print(context) print(reduced_set) print(tree) assert errors == []
def test(): text = """ class Main { main (): Object { 0 }; }; class Point { x: AUTO_TYPE; y: AUTO_TYPE; init(x0: Int, y0: Int): AUTO_TYPE {{ x <- x0; y <- y0; self; }}; }; """ ast = run_pipeline(text) errors = [] collector = TypeCollector(errors) collector.visit(ast) context = collector.context builder = TypeBuilder(context, errors) builder.visit(ast) checker = TypeChecker(context, errors) checker.visit(ast, None) print(errors) if errors != []: print(errors) assert False tset_builder = TSetBuilder(context, errors) tset = tset_builder.visit(ast, None) tset_reducer = TSetReducer(context, errors) reduced_set = tset_reducer.visit(ast, tset) tset_merger = TSetMerger(context, errors) tset_merger.visit(ast, reduced_set) collector = TypeCollector(errors) collector.visit(ast) context = collector.context builder = TypeBuilder(context, errors) builder.visit(ast) checker = TypeChecker(context, errors) checker.visit(ast, None) formatter = FormatVisitor() tree = formatter.visit(ast) print("Errors:", errors) print("Context:") print(context) print(reduced_set) print(tree) assert errors == []
def test(): text = """ class A { a : String ; b : AUTO_TYPE ; c : AUTO_TYPE <- 0 ; d : Object <- while c loop c + 1 pool ; (*first and second errors*) j : AUTO_TYPE ; l : AUTO_TYPE ; fact ( n : AUTO_TYPE ) : AUTO_TYPE { if n < 0 then 1 else n + fact ( n - 1 ) fi } ; step ( p : AUTO_TYPE ) : AUTO_TYPE { b <- { p + 5 ; j <- p ; (*third error*) p <- false ; isvoid d ; l @ Point . main ( ) ; l.main(); } } ; } ; class Point inherits A { h : AUTO_TYPE <- "debe ser tipo string" ; k : AUTO_TYPE ; main ( ) : AUTO_TYPE { let i : AUTO_TYPE <- new A in { isvoid i ; (*Puede lanzar error semantico*) } } ; ackermann ( m : AUTO_TYPE , n : AUTO_TYPE ) : AUTO_TYPE { if m < 0 then 1 else if n < 0 then ackermann ( m - 1 , 1 ) else ackermann ( m - 1 , ackermann ( m , n - 1 ) ) fi fi } ; } ; """ ast = run_pipeline(text) errors = [] collector = TypeCollector(errors) collector.visit(ast) context = collector.context builder = TypeBuilder(context, errors) builder.visit(ast) checker = TypeChecker(context, errors) checker.visit(ast, None) if errors != ["A class Main with a method main most be provided"]: print(errors) assert False tset_builder = TSetBuilder(context, errors) tset = tset_builder.visit(ast, None) tset_reducer = TSetReducer(context, errors) reduced_set = tset_reducer.visit(ast, tset) tset_merger = TSetMerger(context, errors) tset_merger.visit(ast, reduced_set) collector = TypeCollector(errors) collector.visit(ast) context = collector.context builder = TypeBuilder(context, errors) builder.visit(ast) checker = TypeChecker(context, errors) checker.visit(ast, None) formatter = FormatVisitor() tree = formatter.visit(ast) print("Errors:", errors) print("Context:") print(context) print(reduced_set) print(tree) assert errors == [ "A class Main with a method main most be provided", "A class Main with a method main most be provided", "Expression after 'while' must be bool, current is Object", 'Operation is not defined between "Object" and "Int".', 'Cannot convert "Object" into "Int".', ]
def test(): text = """ class Main { main (): Object { 0 }; }; class Ackermann { ackermann(m: AUTO_TYPE, n: AUTO_TYPE): AUTO_TYPE { if m = 0 then n + 1 else if n = 0 then ackermann(m - 1, 1) else ackermann(m - 1, ackermann(m, n - 1)) fi fi }; }; """ ast = run_pipeline(text) errors = [] collector = TypeCollector(errors) collector.visit(ast) context = collector.context builder = TypeBuilder(context, errors) builder.visit(ast) checker = TypeChecker(context, errors) checker.visit(ast, None) print(errors) if errors != []: print(errors) assert False tset_builder = TSetBuilder(context, errors) tset = tset_builder.visit(ast, None) tset_reducer = TSetReducer(context, errors) reduced_set = tset_reducer.visit(ast, tset) tset_merger = TSetMerger(context, errors) tset_merger.visit(ast, reduced_set) collector = TypeCollector(errors) collector.visit(ast) context = collector.context builder = TypeBuilder(context, errors) builder.visit(ast) checker = TypeChecker(context, errors) checker.visit(ast, None) formatter = FormatVisitor() tree = formatter.visit(ast) print("Errors:", errors) print("Context:") print(context) print(reduced_set) print(tree) assert errors == []
def test(): text = """ class A { a : String ; b : Bool ; c : Int <- 0 ; d : Object <- while c < 1 loop c = c + 1 pool ; step ( p : AUTO_TYPE ) : AUTO_TYPE { p . translate ( 1 , 1 ) } ; } ; class Point { step ( p : AUTO_TYPE ) : AUTO_TYPE { p . translate ( 1 , 1 ) } ; main ( ) : Object { let p : AUTO_TYPE <- new Point in { step ( p ) ; (*Puede lanzar error semantico*) } } ; } ; class B inherits A { e : Bool ; step ( p : AUTO_TYPE ) : AUTO_TYPE { p } ; test ( e : Int ) : Bool { { let x : Int <- 4 in e + ~ x ; case 5 + 4 of f : Bool => f ; g : Bool => not g ; esac ; self . step ( e ) ; } } ; } ; class C { a : B ; b : Int <- 8 ; c : Bool <- false ; m ( ) : Bool { { a @ A . step ( b ) ; if not c then true else false fi ; a . step ( b ) ; } } ; } ; """ ast = run_pipeline(text) errors = [] collector = TypeCollector(errors) collector.visit(ast) context = collector.context builder = TypeBuilder(context, errors) builder.visit(ast) checker = TypeChecker(context, errors) scope = checker.visit(ast, None) tset_builder = TSetBuilder(context, errors) tset = tset_builder.visit(ast, None) print("Errors:", errors) print("Context:") print(context) print(tset) assert errors == ["A class Main with a method main most be provided"]
def test(): text = """ class A { b : AUTO_TYPE ; a : AUTO_TYPE ; f ( ) : Int { { let x : AUTO_TYPE <- a + b in x ; } }; }; """ ast = run_pipeline(text) errors = [] collector = TypeCollector(errors) collector.visit(ast) context = collector.context builder = TypeBuilder(context, errors) builder.visit(ast) checker = TypeChecker(context, errors) checker.visit(ast, None) if errors != ["A class Main with a method main most be provided"]: print(errors) assert False tset_builder = TSetBuilder(context, errors) tset = tset_builder.visit(ast, None) tset_reducer = TSetReducer(context, errors) reduced_set = tset_reducer.visit(ast, tset) tset_merger = TSetMerger(context, errors) tset_merger.visit(ast, reduced_set) collector = TypeCollector(errors) collector.visit(ast) context = collector.context builder = TypeBuilder(context, errors) builder.visit(ast) checker = TypeChecker(context, errors) checker.visit(ast, None) formatter = FormatVisitor() tree = formatter.visit(ast) print("Errors:", errors) print("Context:") print(context) print(reduced_set) print(tree) assert errors == [ "A class Main with a method main most be provided", "A class Main with a method main most be provided", ]
def test(): text = """ class Main { a : AUTO_TYPE<- "Hola"; main ( ) : Int { { case {let i: AUTO_TYPE <- 12 in {let i: AUTO_TYPE <- "Hola" in {"Hi"; case 12 of x : Int => x; y : AUTO_TYPE => y; z: String => let i : AUTO_TYPE in { i<- case self of x: Int => 12; x: AUTO_TYPE => if 12<12 then x else "Hola" fi ; esac ;}; esac ;};};} of x : Int => 12; y : AUTO_TYPE => let i: AUTO_TYPE <- y in y ; esac ; a.length();} } ; } ; """ ast = run_pipeline(text) errors = [] collector = TypeCollector(errors) collector.visit(ast) context = collector.context builder = TypeBuilder(context, errors) builder.visit(ast) checker = TypeChecker(context, errors) checker.visit(ast, None) # print(errors) # if errors != []: # print(errors) # assert False tset_builder = TSetBuilder(context, errors) tset = tset_builder.visit(ast, None) tset_reducer = TSetReducer(context, errors) reduced_set = tset_reducer.visit(ast, tset) tset_merger = TSetMerger(context, errors) tset_merger.visit(ast, reduced_set) collector = TypeCollector(errors) collector.visit(ast) context = collector.context builder = TypeBuilder(context, errors) builder.visit(ast) checker = TypeChecker(context, errors) checker.visit(ast, None) formatter = FormatVisitor() tree = formatter.visit(ast) # print("Errors:", errors) # print("Context:") # print(context) # print(reduced_set) # print(tree) final_tree = """__ProgramNode [<class> ... <class>]__ClassDeclarationNode: class Main { <feature> ... <feature> }__AttrDeclarationNode: a : String <- <exp>__ StringNode: Hola__FuncDeclarationNode: main() : Int { <body> }__BlockNode: {<exp>; ... <exp>;}__CaseNode: case <expr> of <case_block> esac__BlockNode: {<exp>; ... <exp>;}__LetNode: let <identif-list> in <expr>__VarDeclarationNode: i : Int <- <expr>__ ConstantNumNode: 12__BlockNode: {<exp>; ... <exp>;}__LetNode: let <identif-list> in <expr>__VarDeclarationNode: i : String <- <expr>__ StringNode: Hola__BlockNode: {<exp>; ... <exp>;}__ StringNode: Hi__CaseNode: case <expr> of <case_block> esac__ ConstantNumNode: 12__CaseItemNode: x : Int => <exp>;__ VariableNode: x__CaseItemNode: y : Object => <exp>;__ VariableNode: y__CaseItemNode: z : String => <exp>;__LetNode: let <identif-list> in <expr>__VarDeclarationNode: i : Object <- <expr>__NONE__BlockNode: {<exp>; ... <exp>;}__AssignNode: i <- <expr>__CaseNode: case <expr> of <case_block> esac__ VariableNode: self__CaseItemNode: x : Int => <exp>;__ ConstantNumNode: 12__CaseItemNode: x : Object => <exp>;__IfNode: if <expr> then <expr> else <exp> fi__<expr> LessNode <expr>__ ConstantNumNode: 12__ ConstantNumNode: 12__ VariableNode: x__ StringNode: Hola__CaseItemNode: x : Int => <exp>;__ ConstantNumNode: 12__CaseItemNode: y : Object => <exp>;__LetNode: let <identif-list> in <expr>__VarDeclarationNode: i : Object <- <expr>__ VariableNode: y__ VariableNode: y__CallNode: <obj>.length(<expr>, ..., <expr>)__ VariableNode: a""" tree = tree.replace("\t", "") tree = tree.replace("\n", "") tree = tree.replace("\\", "") assert tree == final_tree assert errors == []
def run_pipeline(text): main_error1 = ["A class Main with a method main most be provided"] main_error2 = ['"main" method in class Main does not receive any parameters'] # define grammar grammar, idx, string, num = define_cool_grammar() try: tokens = tokenize_cool_text(grammar, idx, string, num, text) parser = LR1Parser(grammar) parse, operations = parser([t.token_type for t in tokens]) # print("\n".join(repr(x) for x in parse)) # print(operations) ast = evaluate_reverse_parse(parse, operations, tokens) formatter = FormatVisitorST() tree = formatter.visit(ast) if initial_ast in selected_options: st.header("Initial Tree:") print_array(tree) errors = [] collector = TypeCollector(errors) collector.visit(ast) context = collector.context builder = TypeBuilder(context, errors) builder.visit(ast) checker = TypeChecker(context, errors) checker.visit(ast, None) if errors != [] and errors != main_error1 and errors != main_error2: st.header("Sorry we found some errors in your code:") print_array(errors) else: if errors == main_error1 or errors == main_error2: st.header("Warning") st.write("We will continue the analisis but note that:") st.write(errors[0]) errors = [] tset_builder = TSetBuilder(context, errors) tset = tset_builder.visit(ast, None) tset_reducer = TSetReducer(context, errors) reduced_set = tset_reducer.visit(ast, tset) tset_merger = TSetMerger(context, errors) tset_merger.visit(ast, reduced_set) collector = TypeCollector(errors) collector.visit(ast) context = collector.context builder = TypeBuilder(context, errors) builder.visit(ast) checker = TypeChecker(context, errors) checker.visit(ast, None) if errors != [] and errors != main_error1 and errors != main_error2: st.header("Sorry we found some errors in your code:") print_array(errors) if reduced_sets in selected_options: st.header("Reduced Sets") print_tset(reduced_set) if final_ast in selected_options: tree = formatter.visit(ast) st.header("Final Tree:") print_array(tree) except Error as error: st.header("Sorry an error occur while tokenizing text:") st.write(str(error))
def test(): text = """ class Main inherits IO { main() : AUTO_TYPE { let x : AUTO_TYPE <- 3 + (let y: AUTO_TYPE <- 8 in y ) in case x of y: Int => out_string("ok"); esac }; }; """ ast = run_pipeline(text) errors = [] collector = TypeCollector(errors) collector.visit(ast) context = collector.context builder = TypeBuilder(context, errors) builder.visit(ast) checker = TypeChecker(context, errors) checker.visit(ast, None) print(errors) if errors != []: print(errors) assert False tset_builder = TSetBuilder(context, errors) tset = tset_builder.visit(ast, None) tset_reducer = TSetReducer(context, errors) reduced_set = tset_reducer.visit(ast, tset) tset_merger = TSetMerger(context, errors) tset_merger.visit(ast, reduced_set) collector = TypeCollector(errors) collector.visit(ast) context = collector.context builder = TypeBuilder(context, errors) builder.visit(ast) checker = TypeChecker(context, errors) checker.visit(ast, None) formatter = FormatVisitor() tree = formatter.visit(ast) print("Errors:", errors) print("Context:") print(context) print(reduced_set) print(tree) assert errors == []
def test(): # text = """ # class A { # f ( a : Int , d : Int ) : Int { # d <- False # }; # }; # class B inherits A { # f ( a : A , d : Int ) : A { # d <- True # }; # }; # """ text = """ class Cons { xcar : Int ; xcdr : String ; isNill ( ) : Bool { false } ; init ( hd : Int , tl : String ) : AUTO_TYPE { { xcar <- hd ; xcdr <- tl ; self; } } ; } ; """ print("corriendo") ast = run_pipeline(text) errors = [] collector = TypeCollector(errors) collector.visit(ast) context = collector.context builder = TypeBuilder(context, errors) builder.visit(ast) checker = TypeChecker(context, errors) checker.visit(ast, None) if errors != ["A class Main with a method main most be provided"]: print(errors) assert False tset_builder = TSetBuilder(context, errors) tset = tset_builder.visit(ast, None) tset_reducer = TSetReducer(context, errors) reduced_set = tset_reducer.visit(ast, tset) tset_merger = TSetMerger(context, errors) tset_merger.visit(ast, reduced_set) collector = TypeCollector(errors) collector.visit(ast) context = collector.context builder = TypeBuilder(context, errors) builder.visit(ast) checker = TypeChecker(context, errors) checker.visit(ast, None) formatter = FormatVisitor() tree = formatter.visit(ast) if errors != [ "A class Main with a method main most be provided", "A class Main with a method main most be provided", ]: print("Errors:", errors) print("Context:") print(context) print(reduced_set) print(tree) assert False final_tree = """__ProgramNode [<class> ... <class>]__ClassDeclarationNode: class Cons { <feature> ... <feature> }__AttrDeclarationNode: xcar : Int <- <exp>__NONE__AttrDeclarationNode: xcdr : String <- <exp>__NONE__FuncDeclarationNode: isNill() : Bool { <body> }__ BooleanNode: false__FuncDeclarationNode: init(hd:Int, tl:String) : Cons { <body> }__BlockNode: {<exp>; ... <exp>;}__AssignNode: xcar <- <expr>__ VariableNode: hd__AssignNode: xcdr <- <expr>__ VariableNode: tl__ VariableNode: self""" tree = tree.replace("\t", "") tree = tree.replace("\n", "") tree = tree.replace("\\", "") print(tree) assert tree == final_tree
def test(): text = """ class Main { pp : AUTO_TYPE; main() : AUTO_TYPE { pp <- self }; }; """ ast = run_pipeline(text) errors = [] collector = TypeCollector(errors) collector.visit(ast) context = collector.context builder = TypeBuilder(context, errors) builder.visit(ast) checker = TypeChecker(context, errors) checker.visit(ast, None) print(errors) if errors != []: print(errors) assert False tset_builder = TSetBuilder(context, errors) tset = tset_builder.visit(ast, None) tset_reducer = TSetReducer(context, errors) reduced_set = tset_reducer.visit(ast, tset) tset_merger = TSetMerger(context, errors) tset_merger.visit(ast, reduced_set) collector = TypeCollector(errors) collector.visit(ast) context = collector.context builder = TypeBuilder(context, errors) builder.visit(ast) checker = TypeChecker(context, errors) checker.visit(ast, None) formatter = FormatVisitor() tree = formatter.visit(ast) print("Errors:", errors) print("Context:") print(context) print(reduced_set) print(tree) assert errors == []