Skip to content
This repository has been archived by the owner on Feb 14, 2021. It is now read-only.

Listo el compilador. #46

Open
wants to merge 133 commits into
base: entrega-final
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
133 commits
Select commit Hold shift + click to select a range
3fa27ca
first-commit
AntonioJesus0398 Nov 22, 2019
9580b1e
Specified the cool grammar
AntonioJesus0398 Feb 7, 2020
6881e99
add lexer
dgc9715 Feb 11, 2020
fb61e9d
Added Grammar and its BNF form
dcordb Feb 11, 2020
cd7e684
Added GitIgnore file
dcordb Feb 14, 2020
db7f06d
Fixed errors in Grammar BNF
dcordb Feb 14, 2020
563e4a9
Fixed, lexer file structure
dcordb Feb 14, 2020
3bb9bce
Merge branch 'parser' into develop
dcordb Feb 14, 2020
30daa47
removed gitignore from parser, now is on develop
dcordb Feb 14, 2020
d2eaf01
Merge branch 'parser' into develop
dcordb Feb 14, 2020
3da2ab6
Added gitignore, now it is right
dcordb Feb 14, 2020
65b8b41
Switched from sly to ply and changed project structure
AntonioJesus0398 Feb 14, 2020
d6e7e7a
Added TYPE rule
AntonioJesus0398 Feb 14, 2020
584e4db
Added INT rule
AntonioJesus0398 Feb 14, 2020
a0bf829
Added BOOL rule
AntonioJesus0398 Feb 14, 2020
0ef634d
Added ==, <=, <-, => rules
AntonioJesus0398 Feb 14, 2020
54b8411
Added simple rules
AntonioJesus0398 Feb 14, 2020
97b2627
Added COMMENT rules
AntonioJesus0398 Feb 14, 2020
1186ac4
Added single line comment rule
AntonioJesus0398 Feb 14, 2020
86bdd0c
Added String rule
AntonioJesus0398 Feb 14, 2020
de4ebd3
Merge branch 'develop' of https://github.com/AntonioJesus0398/Cool-Co…
AntonioJesus0398 Feb 14, 2020
ff1eb09
Merge branch 'lexer' into develop
AntonioJesus0398 Feb 14, 2020
e988785
Merge branch 'develop'
AntonioJesus0398 Feb 14, 2020
5e29b85
Merged the base project with the old project
AntonioJesus0398 Feb 14, 2020
d8c7401
Fixed more stuff in grammar BNF
dcordb Feb 18, 2020
82a5ca2
Fixed more stuff in grammar BNF...
dcordb Feb 18, 2020
0c2fa82
Added comment_eof rule
AntonioJesus0398 Feb 18, 2020
aa78a81
Merge remote-tracking branch 'upstream/entrega-parser' into develop
AntonioJesus0398 Feb 20, 2020
85976ab
Passed all tests except iis4.cl
AntonioJesus0398 Feb 21, 2020
7a05bcb
Changed folder structure and a non terminal in grammar-BNF.txt
dcordb Feb 21, 2020
943229e
Changed multiple stuff in grammar-BNF.txt
dcordb Feb 22, 2020
f9f53dd
Changed multiple stuff in grammar-BNF.txt
dcordb Feb 22, 2020
6d7a3b6
Parser is built, but there are some conflicts ... need to fix this.
dcordb Feb 22, 2020
48845f6
Ok parser is working now, added precedence, no warning (except for th…
dcordb Feb 22, 2020
e26e678
Ready to push
AntonioJesus0398 Feb 22, 2020
75a08f5
Removed unnecessary files
AntonioJesus0398 Feb 22, 2020
41a78ce
Merge branch 'develop' into parser
dcordb Feb 22, 2020
3be5543
Ok parser is working .. ready to push.
dcordb Feb 22, 2020
1c2ed33
All tests passed.
dcordb Feb 23, 2020
0f34679
Cleaned up.
dcordb Feb 23, 2020
522c0d6
Cleaned up.
dcordb Feb 23, 2020
610c508
Added parsetab.py to gitignore
dcordb May 5, 2020
4d97247
Changed file structure. Added main.py. Changed precedence of unary o…
dcordb May 15, 2020
97af555
Changed main.py. Added errors list to parser. Changed cool.sh to exec…
dcordb May 15, 2020
c6f8b4c
Ignored test.cl and parsetab.py
dcordb May 15, 2020
a8263e2
Cleaned up.
dcordb May 15, 2020
3901aeb
Changed main.py, and added new folder to gitignore
dcordb May 18, 2020
78d4444
Changed file reading method.
dcordb May 18, 2020
0ce04de
Added initial code on ast.py
dcordb May 20, 2020
c1e9cd0
Added class ASTNode, changed definition of Formal class to a namedtuple.
dcordb May 20, 2020
c285bf9
Debuging of AST is done
dcordb May 20, 2020
dc82fa1
Changed grammar to appear more acording with AST.
dcordb May 20, 2020
a422408
Integrated AST with parser, still there are few things that need fixi…
dcordb May 20, 2020
220e85b
Fixed AST printing. Changed value of token TYPE in lexer.
dcordb May 21, 2020
d5339d5
Changed Deque by NodeContainer class
dcordb May 21, 2020
74e642b
Addded CaseBranch class and method children in ASTNode.
dcordb May 21, 2020
9a2e1d0
Changed project structure, added unit-tests.
dcordb May 27, 2020
e32b8e9
Added iterative dfs, changed make file. Added new unit-test, this tes…
dcordb May 28, 2020
1d4c0b2
Added two boolean constants (true, false) as instances of Bool.
dcordb May 28, 2020
90bfb78
Added print_tree lib
dcordb Sep 5, 2020
0796808
Added errors file.
dcordb Sep 5, 2020
a7b297a
Made more clear terminals in lexer-parser.
dcordb Sep 5, 2020
5e2e2d0
Recoded main.py, it's more clear now.
dcordb Sep 6, 2020
fe6ccee
Added tab option, now tabs are converted to spaces.
dcordb Sep 7, 2020
bb1eb51
Added environment, error tracking and semantic analysis
dcordb Sep 10, 2020
1513c0a
Fixed nasty sharing data bug, added tests for inheritance tree.
dcordb Sep 11, 2020
5a43218
Added more semantic checks, more tests and set up for type checker.
dcordb Sep 12, 2020
3292bf1
added test
dgc9715 Sep 12, 2020
e78e638
Added new test.
dgc9715 Sep 12, 2020
28bacf3
Made distinctions between case_vars, let_vars and attributes in grammar.
dgc9715 Sep 12, 2020
e15c5ae
Fixed environment, added inheritance check for methods, changed way o…
dgc9715 Sep 13, 2020
7df5b36
Changed error messages.
dgc9715 Sep 13, 2020
a9542ad
Added big part of type_checker code, added new tests.
dcordb Sep 14, 2020
9acaeb2
Added dispatch, if, block, etc. Added SELF_TYPE(C) definitions.
dcordb Sep 15, 2020
07209c3
Added and removed tests, added dummy native functions for Object, IO …
dcordb Sep 15, 2020
f4d8abd
Added case, let and while to type checker. Fixed tests.
dcordb Sep 16, 2020
4bd1b29
Changed a little project folder structure
dcordb Sep 16, 2020
9dec768
Added setrecursionlimit for deep tests, added run_test function.
dcordb Sep 16, 2020
0bd1118
did minor change in _test.py file
dcordb Sep 17, 2020
cf87e5e
Added conversion from COOL to CIL.
dcordb Sep 20, 2020
46695bd
Added static analysis for locals (ie. just computed position relative…
dcordb Sep 21, 2020
201c192
Removed func table, added locals_size for attrs and methods. Set up e…
dcordb Sep 22, 2020
b2bd3e2
Added reserved attributes and changed gen_cil accordingly.
dcordb Sep 24, 2020
73590e0
Merged two files, removed Int, String, Bool particular classes.
dcordb Sep 25, 2020
70a52b1
Added gen_mips, although need to check it some more. Simple cool file…
dcordb Sep 30, 2020
efcad53
Fixed some bugs. Added plus implementation and refined New.
dcordb Sep 30, 2020
4eb59ab
Changed test format.
dgc9715 Oct 1, 2020
5a78fb5
Added checker for test of codegen.
dcordb Oct 1, 2020
e9aa959
Added again let, now the ref compiler script is in unit_tests.
dcordb Oct 2, 2020
7b7fee8
Added all arithmetic operations.
dcordb Oct 2, 2020
879c320
Added bool objects
dcordb Oct 2, 2020
9d4099c
Added boolean binary operators
dcordb Oct 3, 2020
569ebb4
Change eq to a function in mips
dcordb Oct 3, 2020
1e273fc
Added if, while and let.
dcordb Oct 3, 2020
ab3b9a4
Set up stuff for string support.
dcordb Oct 4, 2020
1f02949
Added string basics
dcordb Oct 4, 2020
ba7f1fd
Added int literals to data segment. Added length function.
dcordb Oct 4, 2020
3924434
Added not, isvoid and intcomp.
dcordb Oct 4, 2020
af80f79
Added in_int
dcordb Oct 4, 2020
328a367
Added report
dcordb Oct 4, 2020
d0de870
Added Case
dcordb Oct 5, 2020
b0079ac
Added a test
dcordb Oct 5, 2020
8056c78
Added abort
dgc9715 Oct 6, 2020
7579377
Added copy
dgc9715 Oct 6, 2020
f81aebb
added in string partially
dcordb Oct 7, 2020
5c5d852
Added classes for each primitive type
dcordb Oct 7, 2020
3ff9486
Fixed copy semantics for primitives.
dcordb Oct 9, 2020
77dcfff
Added read string and string init!
dcordb Oct 10, 2020
df1ce00
added string copy!
dcordb Oct 10, 2020
178701c
Added string equality
dgc9715 Oct 10, 2020
00a9c8d
refactored a bit
dgc9715 Oct 10, 2020
6978ead
added formals to native methods
dcordb Oct 10, 2020
1621030
added concat
dcordb Oct 10, 2020
01b506f
Added new constants
dcordb Oct 11, 2020
d7036df
Add substr
dcordb Oct 14, 2020
82aa3b9
Add script to collect runnable tests
dgc9715 Oct 14, 2020
f6a89ee
Check that index greater than size
dgc9715 Oct 14, 2020
6c5de73
Fix tests for final phase
dgc9715 Oct 14, 2020
2436056
Fix bug with void in equality
dcordb Oct 14, 2020
cf6eb1d
Merge branch 'master' into report
dcordb Nov 9, 2020
582f855
Modify report
dcordb Nov 9, 2020
22a4e88
Add structure and syntax analysis to report
dcordb Nov 9, 2020
ecd95cd
Add semantic phase to report
dcordb Nov 9, 2020
22b9c1c
Add type checker info to report
dcordb Nov 9, 2020
b757a15
Modify help message
dcordb Nov 9, 2020
1ca641e
Merge branch 'pseudo_c' into report
dcordb Nov 9, 2020
f32aa6c
Add CIL to report and add documentation to Readme
dcordb Nov 9, 2020
7ba9e89
Add dispatch and case explaining
dcordb Nov 12, 2020
d16bc54
Ok report is done
dcordb Nov 12, 2020
ff89f9b
Add report pdf
dcordb Nov 12, 2020
70e787e
Finish report
dcordb Nov 12, 2020
d38b3f1
Merge branch 'entrega-final' of https://github.com/matcom/cool-compil…
dcordb Nov 25, 2020
4ecb2f0
Passed all tests
dcordb Nov 25, 2020
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
Prev Previous commit
Next Next commit
Added big part of type_checker code, added new tests.
dcordb committed Sep 14, 2020
commit a9542ade6195bcbb5f834046a986da1c70313722
2 changes: 1 addition & 1 deletion src/coolc.sh
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ OUTPUT_FILE=${INPUT_FILE:0: -2}mips

# Si su compilador no lo hace ya, aquí puede imprimir la información de contacto
echo "Cool-Compiler-2020: v1.0"
echo "Copyright (c) 2019: Antonio Jesús Otaño Barrera, Daniel Cordovés Borroto, Denis Gómez Cruz"
echo "Copyright (c) 2019: Antonio Jesús Otaño Barrera, Daniel Enrique Cordovés Borroto, Denis Gómez Cruz"

# Llamar al compilador
#echo "Compiling $INPUT_FILE into $OUTPUT_FILE"
2 changes: 1 addition & 1 deletion src/coolcmp/cmp_utils/lexer.py
Original file line number Diff line number Diff line change
@@ -169,7 +169,7 @@ def t_STRING_end(self, t):
t.lexer.pop_state()
t.type = "STRING"
t.value = String(t.lexer.stringbuf)
t.value.set_tracker(t.lexer.lineno, self.find_column(t))
t.value.set_tracker(t.lexer.lineno, self.find_column(t) - len(t.value.value) - 1)

return t

13 changes: 7 additions & 6 deletions src/coolcmp/cmp_utils/my_ast.py
Original file line number Diff line number Diff line change
@@ -22,13 +22,16 @@ def class_name(self):
return self.__class__.__name__

def __repr__(self):
return self.class_name()
return f'<{self.class_name()}>'

class Formal(ASTNode):
def __init__(self, id, type):
self.id = id
self.type = type

def __repr__(self):
return f'<Formal {self.id}>'

class NodeContainer(deque, ASTNode):
def __repr__(self):
return f'<NodeContainer({len(self)})>'
@@ -56,11 +59,6 @@ def __init__(self, type, opt_inherits, feature_list = NodeContainer(), can_inher
def __repr__(self):
return f'<Class {self.type}>'

class SelfType(Class):
def __init__(self, cls):
self.type = Type('SELF_TYPE') #note that this won't have line and col info
self.cls = cls

class Feature(ASTNode): pass

class Method(Feature):
@@ -93,6 +91,9 @@ def __init__(self, id, expr):
self.id = id
self.expr = expr

def __repr__(self):
return f'<Assignment {self.id}>'

class Dispatch(Expr):
def __init__(self, expr, opt_type, id, expr_list = NodeContainer()):
self.expr = expr
51 changes: 23 additions & 28 deletions src/coolcmp/cmp_utils/parser.py
Original file line number Diff line number Diff line change
@@ -15,13 +15,11 @@ def p_program_2(self, p):
"program : class SEMICOLON"

p[0] = Program(NodeContainer([p[1]]))
p[0].set_tracker(p.lineno(0), self.find_column(p.lexpos(0)))

def p_class(self, p):
"class : CLASS TYPE inherits LBRACE feature_list RBRACE"

p[0] = Class(p[2], p[3], p[5])
p[0].set_tracker(p.lineno(0), self.find_column(p.lexpos(0)))

def p_inherits(self, p):
"""
@@ -68,7 +66,6 @@ def p_formal(self, p):
"formal : ID COLON TYPE"

p[0] = Formal(p[1], p[3])
p[0].set_tracker(p.lineno(0), self.find_column(p.lexpos(0)))

def p_formal_list(self, p):
"formal_list : formal formal_list_helper"
@@ -132,15 +129,13 @@ def p_attribute(self, p):
"attribute : ID COLON TYPE opt_expr_init"

p[0] = Attribute(p[1], p[3], p[4])
p[0].set_tracker(p.lineno(0), self.find_column(p.lexpos(0)))

def p_let_var(self, p):
"""
let_var : ID COLON TYPE opt_expr_init
"""

p[0] = LetVar(p[1], p[3], p[4])
p[0].set_tracker(p.lineno(0), self.find_column(p.lexpos(0)))

def p_let_list(self, p):
"let_list : let_var let_list_helper"
@@ -168,6 +163,7 @@ def p_opt_expr_init(self, p):

if len(p) == 3:
p[0] = p[2]
p[0].set_tracker(p.lineno(1), self.find_column(p.lexpos(1)))

else: p[0] = None

@@ -177,7 +173,6 @@ def p_expr_case_var(self, p):
"""

p[0] = CaseVar(p[1], p[3])
p[0].set_tracker(p.lineno(0), self.find_column(p.lexpos(0)))

def p_case_list(self, p):
"""
@@ -186,7 +181,6 @@ def p_case_list(self, p):
"""

case = CaseBranch(p[1], p[3])
case.set_tracker(p.lineno(0), self.find_column(p.lexpos(0)))

if len(p) == 6:
p[5].appendleft(case)
@@ -198,131 +192,132 @@ def p_expr_assignment(self, p):
"expr : ID ASSIGN expr"

p[0] = Assignment(p[1], p[3])
p[0].set_tracker(p.lineno(0), self.find_column(p.lexpos(0)))
p[0].set_tracker(p.lineno(2), self.find_column(p.lexpos(2)))

def p_expr_dispatch_1(self, p):
"expr : expr CAST TYPE DOT ID LPAREN expr_params RPAREN"

p[0] = Dispatch(p[1], p[3], p[5], p[7])
p[0].set_tracker(p.lineno(0), self.find_column(p.lexpos(0)))
p[0].set_tracker(p.lineno(4), self.find_column(p.lexpos(4)))

def p_expr_dispatch_2(self, p):
"expr : expr DOT ID LPAREN expr_params RPAREN"

p[0] = Dispatch(p[1], None, p[3], p[5])
p[0].set_tracker(p.lineno(0), self.find_column(p.lexpos(0)))
p[0].set_tracker(p.lineno(2), self.find_column(p.lexpos(2)))

def p_expr_self_dispatch(self, p):
"expr : ID LPAREN expr_params RPAREN"

slf = Id('self')
slf.set_tracker(p.lineno(0), self.find_column(p.lexpos(0)))
slf.set_tracker(p.lineno(1), self.find_column(p.lexpos(1)))

p[0] = Dispatch(slf, None, p[1], p[3])
p[0].set_tracker(p.lineno(0), self.find_column(p.lexpos(0)))
p[0].set_tracker(p.lineno(1), self.find_column(p.lexpos(1)))

def p_expr_if(self, p):
"expr : IF expr THEN expr ELSE expr FI"

p[0] = If(p[2], p[4], p[6])
p[0].set_tracker(p.lineno(0), self.find_column(p.lexpos(0)))
p[0].set_tracker(p.lineno(1), self.find_column(p.lexpos(1)))

def p_expr_while(self, p):
"expr : WHILE expr LOOP expr POOL"

p[0] = While(p[2], p[4])
p[0].set_tracker(p.lineno(0), self.find_column(p.lexpos(0)))
p[0].set_tracker(p.lineno(1), self.find_column(p.lexpos(1)))

def p_expr_block(self, p):
"expr : LBRACE expr_list_semicolon RBRACE"

p[0] = Block(p[2])
p[0].set_tracker(p.lineno(0), self.find_column(p.lexpos(0)))
p[0].set_tracker(p.lineno(1), self.find_column(p.lexpos(1)))

# SHIFT/REDUCE conflict with the let expr is intended,
# it is resolved in favor of shifting
def p_expr_let(self, p):
"expr : LET let_list IN expr"

p[0] = Let(p[2], p[4])
p[0].set_tracker(p.lineno(0), self.find_column(p.lexpos(0)))
p[0].set_tracker(p.lineno(1), self.find_column(p.lexpos(1)))

def p_expr_case(self, p):
"expr : CASE expr OF case_list ESAC"

p[0] = Case(p[2], p[4])
p[0].set_tracker(p.lineno(0), self.find_column(p.lexpos(0)))
p[0].set_tracker(p.lineno(1), self.find_column(p.lexpos(1)))

def p_expr_new(self, p):
"expr : NEW TYPE"

p[0] = New(p[2])
p[0].set_tracker(p.lineno(0), self.find_column(p.lexpos(0)))
p[0].set_tracker(p.lineno(1), self.find_column(p.lexpos(1)))

def p_expr_unary_isvoid(self, p):
"expr : ISVOID expr"

p[0] = IsVoid(p[2])
p[0].set_tracker(p.lineno(0), self.find_column(p.lexpos(0)))
p[0].set_tracker(p.lineno(1), self.find_column(p.lexpos(1)))

def p_expr_unary_intcomp(self, p):
"expr : INT_COMP expr"

p[0] = IntComp(p[2])
p[0].set_tracker(p.lineno(0), self.find_column(p.lexpos(0)))
p[0].set_tracker(p.lineno(1), self.find_column(p.lexpos(1)))

def p_expr_unary_not(self, p):
"expr : NOT expr"

p[0] = Not(p[2])
p[0].set_tracker(p.lineno(0), self.find_column(p.lexpos(0)))
p[0].set_tracker(p.lineno(1), self.find_column(p.lexpos(1)))

def p_expr_binary_plus(self, p):
"expr : expr PLUS expr"

p[0] = Plus(p[1], p[3])
p[0].set_tracker(p.lineno(0), self.find_column(p.lexpos(0)))
p[0].set_tracker(p.lineno(2), self.find_column(p.lexpos(2)))

def p_expr_binary_minus(self, p):
"expr : expr MINUS expr"

p[0] = Minus(p[1], p[3])
p[0].set_tracker(p.lineno(0), self.find_column(p.lexpos(0)))
p[0].set_tracker(p.lineno(2), self.find_column(p.lexpos(2)))

def p_expr_binary_mul(self, p):
"expr : expr MUL expr"

p[0] = Mult(p[1], p[3])
p[0].set_tracker(p.lineno(0), self.find_column(p.lexpos(0)))
p[0].set_tracker(p.lineno(2), self.find_column(p.lexpos(2)))

def p_expr_binary_div(self, p):
"expr : expr DIV expr"

p[0] = Div(p[1], p[3])
p[0].set_tracker(p.lineno(0), self.find_column(p.lexpos(0)))
p[0].set_tracker(p.lineno(2), self.find_column(p.lexpos(2)))

def p_expr_binary_less(self, p):
"expr : expr LESS expr"

p[0] = Less(p[1], p[3])
p[0].set_tracker(p.lineno(0), self.find_column(p.lexpos(0)))
p[0].set_tracker(p.lineno(2), self.find_column(p.lexpos(2)))

def p_expr_binary_lesseq(self, p):
"expr : expr LESS_EQ expr"

p[0] = LessEq(p[1], p[3])
p[0].set_tracker(p.lineno(0), self.find_column(p.lexpos(0)))
p[0].set_tracker(p.lineno(2), self.find_column(p.lexpos(2)))

def p_expr_binary_eq(self, p):
"expr : expr EQ expr"

p[0] = Eq(p[1], p[3])
p[0].set_tracker(p.lineno(0), self.find_column(p.lexpos(0)))
p[0].set_tracker(p.lineno(2), self.find_column(p.lexpos(2)))

def p_expr_paren(self, p):
"expr : LPAREN expr RPAREN"

p[0] = p[2]
p[0].set_tracker(p.lineno(1), self.find_column(p.lexpos(1)))

def p_expr_id(self, p):
"expr : ID"
12 changes: 11 additions & 1 deletion src/coolcmp/cmp_utils/semantics.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from coolcmp.cmp_utils.errors import *
from coolcmp.cmp_utils.my_ast import *
from coolcmp.cmp_utils.visitor import Visitor
from coolcmp.cmp_utils.environment import Environment

class SemanticAnalyzer:
@@ -60,6 +59,17 @@ def build_inheritance_tree(self, native_classes):
if 'main' not in main_class.methods:
raise SemanticError(main_class.type.line, main_class.type.col, f'Couldnt find <Method {Id("main")}()> on {main_class}')

ref = main_class.methods['main']

if len(ref.get_signature()) > 1:
raise SemanticError(ref.id.line, ref.id.col, f'{ref} must have no formal parameters')

if 'SELF_TYPE' in cls_refs:
ref = cls_refs['SELF_TYPE']
raise SemanticError(ref.type.line, ref.type.col, f'Tried to declare {ref}')

cls_refs['SELF_TYPE'] = Class(Type('SELF_TYPE'), None)

return cls_refs

def check_cycles(self):
3 changes: 2 additions & 1 deletion src/coolcmp/cmp_utils/source_code.py
Original file line number Diff line number Diff line change
@@ -55,4 +55,5 @@ def semanticAnalysis(self, ast_root):
semantics.check_cycles()

def runTypeChecker(self):
chk = TypeChecker(self.root, self.cls_refs)
chk = TypeChecker(self.root, self.cls_refs)
chk.visit(self.root)
Loading