|
|
|
@ -5,6 +5,31 @@ import sys
|
|
|
|
|
def find(needle: str, haystack: str) -> bool:
|
|
|
|
|
return haystack.find(needle) != -1
|
|
|
|
|
|
|
|
|
|
def parse_lparen(line: str) -> list:
|
|
|
|
|
tokens = list()
|
|
|
|
|
split_on_lparen = line.split('(')
|
|
|
|
|
name = split_on_lparen[0].strip()
|
|
|
|
|
if find('=', line):
|
|
|
|
|
name = name.split('=')[1].strip()
|
|
|
|
|
without_name = '('.join(split_on_lparen[1:])
|
|
|
|
|
split_on_rparen = without_name.split(')')
|
|
|
|
|
params_str = ')'.join(split_on_rparen[:-1])
|
|
|
|
|
params = list(map(str.strip,params_str.split(',')))
|
|
|
|
|
if name not in ['if', 'while']:
|
|
|
|
|
function_call = {"variant": "function_call", "value": name, "params": params, "line_number": lineno}
|
|
|
|
|
tokens.append(function_call)
|
|
|
|
|
elif name == "if":
|
|
|
|
|
conditional = {"variant": "conditional", "value": name, "condition": ','.join(params), "line_number": lineno}
|
|
|
|
|
tokens.append(conditional)
|
|
|
|
|
expression = {"variant": "start_block", "value": "{", "line_number": lineno}
|
|
|
|
|
tokens.append(expression)
|
|
|
|
|
else:
|
|
|
|
|
loop = {"variant": "loop", "value": name, "condition": ','.join(params), "line_number": lineno}
|
|
|
|
|
tokens.append(loop)
|
|
|
|
|
expression = {"variant": "start_block", "value": "{", "line_number": lineno}
|
|
|
|
|
tokens.append(expression)
|
|
|
|
|
return tokens
|
|
|
|
|
|
|
|
|
|
source: list = list()
|
|
|
|
|
program = './prog.mn'
|
|
|
|
|
if len(sys.argv) > 1:
|
|
|
|
@ -44,14 +69,19 @@ for lineno in range(1, num_lines + 1):
|
|
|
|
|
signifier = {"variant": "function_declaration", "value": name, "params": params, "line_number": lineno}
|
|
|
|
|
tokens.append(signifier)
|
|
|
|
|
if find('=', line):
|
|
|
|
|
expressions = list()
|
|
|
|
|
colon_equals_rhs = '='.join(colon_rhs_split_on_equals[1:])
|
|
|
|
|
operator = {"variant": "operator", "value": '=', "line_number": lineno}
|
|
|
|
|
tokens.append(operator)
|
|
|
|
|
expr = colon_equals_rhs.strip()
|
|
|
|
|
expression = {"variant": "expression", "value": expr, "line_number": lineno}
|
|
|
|
|
expressions.append({"variant": "expression", "value": expr, "line_number": lineno})
|
|
|
|
|
if expr == '{':
|
|
|
|
|
expression = {"variant": "start_block", "value": expr, "line_number": lineno}
|
|
|
|
|
tokens.append(expression)
|
|
|
|
|
expressions[0] = {"variant": "start_block", "value": expr, "line_number": lineno}
|
|
|
|
|
elif find('(', line) and find(')', line):
|
|
|
|
|
expressions = parse_lparen(line)
|
|
|
|
|
for expression in expressions:
|
|
|
|
|
tokens.append(expression)
|
|
|
|
|
|
|
|
|
|
elif find('=', line):
|
|
|
|
|
equals_rhs = '='.join(split_on_equals[1:])
|
|
|
|
|
signifier = {"variant": "signifier", "value": equals_lhs.strip(), "line_number": lineno}
|
|
|
|
@ -67,25 +97,12 @@ for lineno in range(1, num_lines + 1):
|
|
|
|
|
expression = {"variant": "end_block", "value": '}', "line_number": lineno}
|
|
|
|
|
tokens.append(expression)
|
|
|
|
|
elif find('(', line):
|
|
|
|
|
split_on_lparen = line.split('(')
|
|
|
|
|
name = split_on_lparen[0].strip()
|
|
|
|
|
without_name = '('.join(split_on_lparen[1:])
|
|
|
|
|
split_on_rparen = without_name.split(')')
|
|
|
|
|
params_str = ')'.join(split_on_rparen[:-1])
|
|
|
|
|
params = list(map(str.strip,params_str.split(',')))
|
|
|
|
|
if name not in ['if', 'while']:
|
|
|
|
|
function_call = {"variant": "function_call", "value": name, "params": params, "line_number": lineno}
|
|
|
|
|
tokens.append(function_call)
|
|
|
|
|
elif name == "if":
|
|
|
|
|
conditional = {"variant": "conditional", "value": name, "condition": ','.join(params), "line_number": lineno}
|
|
|
|
|
tokens.append(conditional)
|
|
|
|
|
expression = {"variant": "start_block", "value": "{", "line_number": lineno}
|
|
|
|
|
tokens.append(expression)
|
|
|
|
|
else:
|
|
|
|
|
loop = {"variant": "loop", "value": name, "condition": ','.join(params), "line_number": lineno}
|
|
|
|
|
tokens.append(loop)
|
|
|
|
|
expression = {"variant": "start_block", "value": "{", "line_number": lineno}
|
|
|
|
|
for expression in parse_lparen(line):
|
|
|
|
|
tokens.append(expression)
|
|
|
|
|
elif find('return', line):
|
|
|
|
|
rhs = line.strip().split()[1]
|
|
|
|
|
expression = {"variant": "return", "value": rhs, "line_number": lineno}
|
|
|
|
|
tokens.append(expression)
|
|
|
|
|
else:
|
|
|
|
|
if line.strip() == '{':
|
|
|
|
|
expression = {"variant": "start_block", "value": '{', "line_number": lineno}
|
|
|
|
|