-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopp2c.py
24 lines (22 loc) · 852 Bytes
/
opp2c.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from lark import Lark
import sys
sys.stdin.reconfigure(encoding='utf-8')
def translate(t):
if t.data == 'start':
return ''.join(map(translate, t.children))
elif t.data == 'halt':
return 'printf("%d %d %d %d %d\\n",r[0],r[1],r[2],r[3],r[4]);exit(0);'
elif t.data[0:3] == 'inc':
return 'r['+str(int(t.data[3])-1)+']++;'
elif t.data[0:3] == 'dec':
return 'r['+str(int(t.data[3])-1)+']--;'
elif t.data[0:4] == 'loop':
return 'while(r['+str(int(t.data[4])-1)+']){' + ''.join(map(translate, t.children)) + '}'
else:
raise SyntaxError("bad tree")
with open('ogham.lark', 'r', encoding="utf-8") as f:
my_grammar = f.read()
parser = Lark(my_grammar)
program = sys.stdin.read()
parse_tree = parser.parse(program)
print("#include <stdio.h>\n#include <stdlib.h>\nint main(){int r[5]={0,0,0,0,0};"+translate(parse_tree)+'}')