Skip to content

Commit

Permalink
Working w/ normal evaluating
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielzezze committed Jun 1, 2021
1 parent e8d9a58 commit eee7d50
Show file tree
Hide file tree
Showing 11 changed files with 3,136 additions and 2,738 deletions.
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ name = "pypi"

[packages]
sly = "*"
llvmlite = "*"

[dev-packages]

Expand Down
29 changes: 28 additions & 1 deletion Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 17 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,52 @@
import sys
from src.Types.TokenTypes import TokenTypes
from src.SymbolTable import SymbolTable
from src.Parser import ZParser
from src.Tokenizer import ZTokenizer
from src.Codegen.CodeGen import CodeGen

if __name__ == '__main__':

if len(sys.argv) < 2:
raise ValueError("No input file")

# Get file path and read content
file_path = sys.argv[1]

if file_path[-2:] != '.z':
raise ValueError('Invalid file type')

with open(file=file_path, mode='r') as file:
file_content = file.read()

# Instantiate lexer and parse
lexer = ZTokenizer()
parser = ZParser()
codegen = CodeGen()

# Get tokens from file
tokens = lexer.tokenize(file_content)
with open('test_tokens.out', 'w') as tmp:
for token in tokens:
tmp.write(str(token) + '\n')

# Tokenize and parse language
tokens = lexer.tokenize(file_content)

root = parser.parse(tokens)

# Set functions declarations to symbol table
symbol_table = SymbolTable()
root.Evaluate(symbol_table)

# Evaluate main function
main_func = symbol_table.get_function('main')
main_func_node = main_func.get("value", None)
if main_func_node is None:
raise ValueError('main function was not defined')
returned_data = main_func_node.statements.Evaluate(symbol_table)

# Check main function returned data type
if returned_data is None:
raise ValueError('main function did not return an int')

main_func_node.statements.Evaluate(symbol_table)
returned_type = returned_data[0]
if returned_type != TokenTypes.INT:
raise ValueError('main did not return an int')
65 changes: 65 additions & 0 deletions src/Codegen/CodeGen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from llvmlite import ir, binding


class CodeGen():
def __init__(self):
self.binding = binding
self.binding.initialize()
self.binding.initialize_native_target()
self.binding.initialize_native_asmprinter()
self._config_llvm()
self._create_execution_engine()
self._declare_print_function()

def _config_llvm(self):
# Config LLVM
self.module = ir.Module(name=__file__)
self.module.triple = self.binding.get_default_triple()
func_type = ir.FunctionType(ir.VoidType(), [], False)
base_func = ir.Function(self.module, func_type, name="main")
block = base_func.append_basic_block(name="entry")
self.builder = ir.IRBuilder(block)

def _create_execution_engine(self):
"""
Create an ExecutionEngine suitable for JIT code generation on
the host CPU. The engine is reusable for an arbitrary number of
modules.
"""
target = self.binding.Target.from_default_triple()
target_machine = target.create_target_machine()
# And an execution engine with an empty backing module
backing_mod = binding.parse_assembly("")
engine = binding.create_mcjit_compiler(backing_mod, target_machine)
self.engine = engine

def _declare_print_function(self):
# Declare Printf function
voidptr_ty = ir.IntType(8).as_pointer()
printf_ty = ir.FunctionType(ir.IntType(32), [voidptr_ty], var_arg=True)
printf = ir.Function(self.module, printf_ty, name="zPrint")
self.printf = printf

def _compile_ir(self):
"""
Compile the LLVM IR string with the given engine.
The compiled module object is returned.
"""
# Create a LLVM module object from the IR
self.builder.ret_void()
llvm_ir = str(self.module)
mod = self.binding.parse_assembly(llvm_ir)
mod.verify()
# Now add the module and make sure it is ready for execution
self.engine.add_module(mod)
self.engine.finalize_object()
self.engine.run_static_constructors()
return mod

def create_ir(self):
self._compile_ir()

def save_ir(self, filename):
with open(filename, 'w') as output_file:
output_file.write(str(self.module))

4 changes: 2 additions & 2 deletions src/Nodes/FuncArguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
from src.SymbolTable import SymbolTable

class FuncArguments(Node):
def __init__(self, func_name):
def __init__(self, func_name, return_type):
self.args = {
func_name: { "value": None }
func_name: { "value": None, "type": return_type }
}
super().__init__(
value=func_name,
Expand Down
20 changes: 19 additions & 1 deletion src/Nodes/FuncCall.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from src.Types.TokenTypes import TokenTypes
from src.Nodes.Return import ReturnStatement
from src.Node import Node
from src.SymbolTable import SymbolTable
Expand Down Expand Up @@ -35,8 +36,25 @@ def Evaluate(self, symbol_table: SymbolTable):
func_symbol_table.set(key, type, value)


func_key = list(func_args_keys)[0]
return_type = func_node.args.args[func_key].get('type', None).type
returned_data = func_node.statements.Evaluate(symbol_table=func_symbol_table)
return returned_data

if returned_data is None:
raise ValueError(f'Function {func_key} must return {return_type}')

returned_type = returned_data[0]
returned_value = returned_data[1]

if returned_type == TokenTypes.INT and return_type != TokenTypes.INT:
raise ValueError(f'Function {func_key} returned incorrect type')
elif returned_type == TokenTypes.STRING and return_type != TokenTypes.STRING_TYPE:
raise ValueError(f'Function {func_key} returned incorrect type')
elif returned_type in [TokenTypes.FALSE, TokenTypes.TRUE] and return_type != TokenTypes.BOOL_TYPE:
raise ValueError(f'Function {func_key} returned incorrect type')


return returned_type, returned_value


raise ValueError(f'Function {self.value} was not declared')
18 changes: 13 additions & 5 deletions src/Parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,27 @@ def func_def(self, p):


@_(
'FUNCTION IDENTIFIER PARENTHESIS_OPEN { argument } PARENTHESIS_CLOSE block EOL'
'BOOL_TYPE FUNCTION IDENTIFIER PARENTHESIS_OPEN { argument } PARENTHESIS_CLOSE block EOL',
'STRING_TYPE FUNCTION IDENTIFIER PARENTHESIS_OPEN { argument } PARENTHESIS_CLOSE block EOL',
'INT FUNCTION IDENTIFIER PARENTHESIS_OPEN { argument } PARENTHESIS_CLOSE block EOL'
)
def function(self, p):
values = p._slice
node = NoOp()

if values[0].type == 'FUNCTION':
func_args = FuncArguments(func_name=values[1].value)
if len(values) > 6:
if values[1].type == 'FUNCTION':
return_type = TokenTypes.INT
if values[0].type == 'BOOL_TYPE':
return_type = TokenTypes.BOOL_TYPE
elif values[0].type == 'STRING_TYPE':
return_type = TokenTypes.STRING_TYPE

func_args = FuncArguments(func_name=values[2].value, return_type=Token(value='', token_type=return_type))
if len(values) > 7:
for arg in p.argument:
func_args.add_argument(name=arg.value, type=arg.type)

return FuncDeclaration(func_name=values[1].value, statements=p.block, args=func_args)
return FuncDeclaration(func_name=values[2].value, statements=p.block, args=func_args)

return node

Expand Down
2 changes: 1 addition & 1 deletion syntax/EBNF
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ IDENTIFIER = ALPHACHAR, { ALPHACHAR | NUMERIC | "_" };
EOL = ".z";
TYPE = "numz" | "bool" | "charz"

FUNC = "func", IDENTIFIER, "(", { ARG }, ")", BLOCK, EOL;
FUNC = TYPE "func", IDENTIFIER, "(", { ARG }, ")", BLOCK, EOL;
ARG = TYPE, IDENTIFIER;

BLOCK = "{", [ACTION], "}";
Expand Down
Loading

0 comments on commit eee7d50

Please sign in to comment.