parent
c38f5ffacc
commit
62718009e9
@ -1,134 +0,0 @@
|
||||
[
|
||||
{
|
||||
"variant": "type",
|
||||
"value": "func",
|
||||
"line_number": 1
|
||||
},
|
||||
{
|
||||
"variant": "operator",
|
||||
"value": ":",
|
||||
"line_number": 1
|
||||
},
|
||||
{
|
||||
"variant": "function_declaration",
|
||||
"value": "hello",
|
||||
"params": [
|
||||
{
|
||||
"type": "string",
|
||||
"name": "x"
|
||||
}
|
||||
],
|
||||
"line_number": 1
|
||||
},
|
||||
{
|
||||
"variant": "operator",
|
||||
"value": "=",
|
||||
"line_number": 1
|
||||
},
|
||||
{
|
||||
"variant": "start_block",
|
||||
"value": "{",
|
||||
"line_number": 1
|
||||
},
|
||||
{
|
||||
"variant": "type",
|
||||
"value": "stdout",
|
||||
"line_number": 2
|
||||
},
|
||||
{
|
||||
"variant": "operator",
|
||||
"value": ":",
|
||||
"line_number": 2
|
||||
},
|
||||
{
|
||||
"variant": "signifier",
|
||||
"value": "out",
|
||||
"line_number": 2
|
||||
},
|
||||
{
|
||||
"variant": "operator",
|
||||
"value": "=",
|
||||
"line_number": 2
|
||||
},
|
||||
{
|
||||
"variant": "expression",
|
||||
"value": "x",
|
||||
"line_number": 2
|
||||
},
|
||||
{
|
||||
"variant": "end_block",
|
||||
"value": "}",
|
||||
"line_number": 3
|
||||
},
|
||||
{
|
||||
"variant": "type",
|
||||
"value": "string",
|
||||
"line_number": 5
|
||||
},
|
||||
{
|
||||
"variant": "operator",
|
||||
"value": ":",
|
||||
"line_number": 5
|
||||
},
|
||||
{
|
||||
"variant": "signifier",
|
||||
"value": "name",
|
||||
"line_number": 5
|
||||
},
|
||||
{
|
||||
"variant": "signifier",
|
||||
"value": "name",
|
||||
"line_number": 6
|
||||
},
|
||||
{
|
||||
"variant": "operator",
|
||||
"value": "=",
|
||||
"line_number": 6
|
||||
},
|
||||
{
|
||||
"variant": "expression",
|
||||
"value": "hej hopp",
|
||||
"line_number": 6
|
||||
},
|
||||
{
|
||||
"variant": "function_call",
|
||||
"value": "hello",
|
||||
"params": [
|
||||
"name"
|
||||
],
|
||||
"line_number": 9
|
||||
},
|
||||
{
|
||||
"variant": "type",
|
||||
"value": "string",
|
||||
"line_number": 12
|
||||
},
|
||||
{
|
||||
"variant": "operator",
|
||||
"value": ":",
|
||||
"line_number": 12
|
||||
},
|
||||
{
|
||||
"variant": "signifier",
|
||||
"value": "micke",
|
||||
"line_number": 12
|
||||
},
|
||||
{
|
||||
"variant": "operator",
|
||||
"value": "=",
|
||||
"line_number": 12
|
||||
},
|
||||
{
|
||||
"variant": "expression",
|
||||
"value": "Micke Nordin",
|
||||
"line_number": 12
|
||||
},
|
||||
{
|
||||
"variant": "function_call",
|
||||
"value": "hello",
|
||||
"params": [
|
||||
"micke"
|
||||
],
|
||||
"line_number": 14
|
||||
}
|
||||
]
|
@ -0,0 +1,44 @@
|
||||
#!/usr/bin/env python3
|
||||
import json
|
||||
import sys
|
||||
tokens = dict()
|
||||
program = './ast.json'
|
||||
if len(sys.argv) > 1:
|
||||
program = sys.argv[1]
|
||||
with open(program, 'r') as json_tokens:
|
||||
tokens = json.loads(json_tokens.read())
|
||||
|
||||
def throw(token: dict, message):
|
||||
print("{} on line {} ".format(message, token['line_number']))
|
||||
print("\033[4m" + token['value'] + "\033[0m")
|
||||
sys.exit(1)
|
||||
|
||||
def parse_expression(params, expression):
|
||||
split_on_space = expression.split(" ")
|
||||
if len(split_on_space) == 1:
|
||||
for param in params:
|
||||
if param['signifier'] == expression and param['type'] == 'string':
|
||||
return param['expression']
|
||||
else:
|
||||
return "Unknown"
|
||||
|
||||
|
||||
for token in tokens:
|
||||
if token['variant'] == "syntax_error":
|
||||
throw(token, "Syntax error")
|
||||
if token['variant'] == "function_call":
|
||||
args = token['params']
|
||||
function = token['function']
|
||||
params = function['params']
|
||||
if len(args) != len(params):
|
||||
throw(token, "Wrong number of arguments")
|
||||
for index in range(0, len(args)):
|
||||
if args[index]['type'] != params[index]['type']:
|
||||
throw(token, "Type missmatch on arguments")
|
||||
signifier = params[index]['name']
|
||||
params[index] = args[index]
|
||||
params[index]['signifier'] = signifier
|
||||
for part in function['body']:
|
||||
if part['type'] == "stdout":
|
||||
print(parse_expression(params, part['expression']))
|
||||
|
@ -0,0 +1,29 @@
|
||||
#!/bin/bash
|
||||
|
||||
program=${1}
|
||||
command=${2}
|
||||
if [[ "x${program}" == "x" ]]; then
|
||||
usage "${0} <program>.mn [lex|parse|run]"
|
||||
exit 1
|
||||
fi
|
||||
if [[ "x${command}" == "x" ]]; then
|
||||
command="run"
|
||||
fi
|
||||
|
||||
if [[ "${command}" == "lex" ]]; then
|
||||
./lexer.py ${program} | jq . > ${program}.tokens && cat ${program}.tokens
|
||||
rm ${program}.tokens
|
||||
exit 0
|
||||
fi
|
||||
if [[ "${command}" == "parse" ]]; then
|
||||
./lexer.py ${program} | jq . > ${program}.tokens && ./parser.py ${program}.tokens | jq . > ${program}.ast && cat ${program}.ast
|
||||
rm ${program}.tokens ${program}.ast
|
||||
exit 0
|
||||
fi
|
||||
if [[ "${command}" == "run" ]]; then
|
||||
./lexer.py ${program} | jq . > ${program}.tokens && ./parser.py ${program}.tokens | jq . > ${program}.ast && ./interpreter.py ${program}.ast
|
||||
rm ${program}.tokens ${program}.ast
|
||||
exit 0
|
||||
fi
|
||||
usage "${0} <program>.mn [lex|parse|run]"
|
||||
exit 1
|
@ -1,28 +0,0 @@
|
||||
float: x = 10.1
|
||||
|
||||
func: hello(float: x) = {
|
||||
stdout: out = x * 10
|
||||
}
|
||||
|
||||
{
|
||||
str: name
|
||||
int: var = 1
|
||||
name = hej hopp
|
||||
}
|
||||
|
||||
|
||||
hello(x)
|
||||
|
||||
func: good_bye(void: none) = {
|
||||
int: getout = 0
|
||||
}
|
||||
good_bye()
|
||||
|
||||
if (x ? 1) {
|
||||
hello(x)
|
||||
}
|
||||
|
||||
while (x < 1) {
|
||||
x = x -1
|
||||
hello(x)
|
||||
}
|
@ -1 +0,0 @@
|
||||
[{"variant": "type", "value": "float", "line_number": 1}, {"variant": "operator", "value": ":", "line_number": 1}, {"variant": "signifier", "value": "x", "line_number": 1}, {"variant": "operator", "value": "=", "line_number": 1}, {"variant": "expression", "value": "10.1", "line_number": 1}, {"variant": "type", "value": "func", "line_number": 3}, {"variant": "operator", "value": ":", "line_number": 3}, {"variant": "function_declaration", "value": "hello", "params": [{"type": "float", "name": "x"}], "line_number": 3}, {"variant": "operator", "value": "=", "line_number": 3}, {"variant": "start_block", "value": "{", "line_number": 3}, {"variant": "type", "value": "stdout", "line_number": 4}, {"variant": "operator", "value": ":", "line_number": 4}, {"variant": "signifier", "value": "out", "line_number": 4}, {"variant": "operator", "value": "=", "line_number": 4}, {"variant": "expression", "value": "x * 10", "line_number": 4}, {"variant": "end_block", "value": "}", "line_number": 5}, {"variant": "start_block", "value": "{", "line_number": 7}, {"variant": "type", "value": "str", "line_number": 8}, {"variant": "operator", "value": ":", "line_number": 8}, {"variant": "signifier", "value": "name", "line_number": 8}, {"variant": "type", "value": "int", "line_number": 9}, {"variant": "operator", "value": ":", "line_number": 9}, {"variant": "signifier", "value": "var", "line_number": 9}, {"variant": "operator", "value": "=", "line_number": 9}, {"variant": "expression", "value": "1", "line_number": 9}, {"variant": "signifier", "value": "name", "line_number": 10}, {"variant": "operator", "value": "=", "line_number": 10}, {"variant": "expression", "value": "hej hopp", "line_number": 10}, {"variant": "end_block", "value": "}", "line_number": 11}, {"variant": "function_call", "value": "hello", "params": ["x"], "line_number": 14}, {"variant": "type", "value": "func", "line_number": 16}, {"variant": "operator", "value": ":", "line_number": 16}, {"variant": "function_declaration", "value": "good_bye", "params": [{"type": "void", "name": "none"}], "line_number": 16}, {"variant": "operator", "value": "=", "line_number": 16}, {"variant": "start_block", "value": "{", "line_number": 16}, {"variant": "type", "value": "int", "line_number": 17}, {"variant": "operator", "value": ":", "line_number": 17}, {"variant": "signifier", "value": "getout", "line_number": 17}, {"variant": "operator", "value": "=", "line_number": 17}, {"variant": "expression", "value": "0", "line_number": 17}, {"variant": "end_block", "value": "}", "line_number": 18}, {"variant": "function_call", "value": "good_bye", "params": [""], "line_number": 19}, {"variant": "conditional", "value": "if", "condition": "x ? 1", "line_number": 21}, {"variant": "start_block", "value": "{", "line_number": 21}, {"variant": "function_call", "value": "hello", "params": ["x"], "line_number": 22}, {"variant": "end_block", "value": "}", "line_number": 23}, {"variant": "loop", "value": "while", "condition": "x < 1", "line_number": 25}, {"variant": "start_block", "value": "{", "line_number": 25}, {"variant": "signifier", "value": "x", "line_number": 26}, {"variant": "operator", "value": "=", "line_number": 26}, {"variant": "expression", "value": "x -1", "line_number": 26}, {"variant": "function_call", "value": "hello", "params": ["x"], "line_number": 27}, {"variant": "end_block", "value": "}", "line_number": 28}]
|
Loading…
Reference in new issue