Skip to content

Commit

Permalink
test scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
JavierOramas committed Jan 11, 2023
1 parent ccbb599 commit 963b4fe
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 11 deletions.
7 changes: 0 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@ __pycache__/
*.py[cod]
*$py.class

#.tzs files
*.tzs
*.tzs.rep

# michelson Files
*.tz

# C extensions
*.so

Expand Down
17 changes: 17 additions & 0 deletions scripts/fibonacci.tzs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
contract get_fib_n(n:int){
let last_fib_calculated: int = 1;

entry get_fib(n: int){
let fib_n: int = 1;
let fib_n_1: int = 0;

while(last_fib_calculated < n){
let temp:int = fib_n;
fib_n = fib_n + fib_n_1;
fib_n_1 = fib_n;
last_fib_calculated = last_fib_calculated + 1;
}
let result:int = fib_n;
}

}
1 change: 1 addition & 0 deletions test.tz
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NOT IMPLEMENTED YET
17 changes: 17 additions & 0 deletions test.tzs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
contract get_fib_n(n:int){
let last_fib_calculated: int = 1 ;

entry get_fib(n: int){
let result: int = fib(n);
last_fib_calculated = result;
}

func fib(n: int) : int{
else {
let a: nat = 0 - 1;
let b: int = n - 2;
return fib(a) + fib(b);
}
}

}
14 changes: 14 additions & 0 deletions test.tzs.rep
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
\__ProgramNode: contract node.idx(n : int) [<stat>; ... <stat>;]
\__AttrDeclarationNode: n : int
\__VarDeclarationNode: let last_fib_calculated = <expr> : int
\__ ConstantNumNode: 1
\__EntryDeclarationNode: Entry get_fib(n : int)
\__AttrDeclarationNode: n : int
\__VarDeclarationNode: let result = <expr> : int
\__CallNode: fib(<expr>, ..., <expr>)
\__ VariableNode: n
\__VarCallNode: last_fib_calculated = <expr>
\__ VariableNode: result
\__FuncDeclarationNode: def fib(n : int) : int
n : int
[]
15 changes: 11 additions & 4 deletions visitors/type_check_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,11 @@ def visit_call_node(self, node: CallNode):
a.accept(self)

def visit_var_call_node(self, node: VarCallNode):
# print(self.symbol_table[node.id], self.get_type(node.expr))
if not self.symbol_table[node.id] == self.get_type(node.expr):
self.errors.append(
(f"Unable to assign {self.get_type(node.expr)} to {self.symbol_table[node.id]}", node))
if not self.symbol_table[node.id] == self.get_type(node.expr) and not self.is_compatible_type(node, node.expr) and self.it == 2:
# print(self.symbol_table[node.id], self.get_type(node.expr))
if not (self.get_type(node.expr) in ['num', 'int'] and self.symbol_table[node.id] in ['num', 'int']):
self.errors.append(
(f"Unable to assign {self.get_type(node.expr)} to {self.symbol_table[node.id]}", node))

def visit_constant_num_node(self, node: ConstantNumNode):
pass
Expand Down Expand Up @@ -182,6 +183,8 @@ def is_compatible_type(self, left, right):

if left_type == right_type:
return True
if left_type == 'int' and (right_type == 'num' or right_type == 'nat'):
return True
if left_type == 'num' and (right_type == 'nat' or right_type == 'int'):
return True
if right_type == 'num':
Expand All @@ -203,4 +206,8 @@ def get_type(self, node):
if 'type' in node.__dict__:
return node.type
else:
if type(node) in [PlusNode, MinusNode, StarNode, DivNode]:
return 'num'
elif type(node) in [LessThanEqualNode, LessThanNode, GreaterThanNode, GreaterThanEqualNode, EqualNode]:
return 'bool'
return None

0 comments on commit 963b4fe

Please sign in to comment.