2023-10-24 14:54:00 -07:00
|
|
|
#! python3
|
2023-10-12 22:46:37 -07:00
|
|
|
import os
|
2023-10-24 14:54:00 -07:00
|
|
|
import pathlib
|
2023-10-12 22:46:37 -07:00
|
|
|
import sys
|
|
|
|
|
2023-10-24 14:54:00 -07:00
|
|
|
from jinja2 import Environment, FileSystemLoader
|
|
|
|
|
2023-10-13 00:15:31 -07:00
|
|
|
from parser import *
|
2023-10-12 22:46:37 -07:00
|
|
|
|
|
|
|
def main():
|
|
|
|
if len(sys.argv) != 2:
|
|
|
|
sys.exit("Takes name of file.")
|
|
|
|
|
|
|
|
filename = sys.argv[1]
|
2023-10-25 21:53:55 -07:00
|
|
|
filebase = os.path.basename(filename)
|
2023-10-12 22:46:37 -07:00
|
|
|
|
2023-10-13 15:04:25 -07:00
|
|
|
ast = None
|
2023-10-12 22:46:37 -07:00
|
|
|
with open(filename, mode='r') as f:
|
|
|
|
filedata = f.read()
|
|
|
|
lexemes = lexer(filedata)
|
|
|
|
|
|
|
|
parser = Parser(lexemes)
|
2023-10-12 23:14:25 -07:00
|
|
|
ast = parser.parse()
|
|
|
|
type_check(ast)
|
2023-10-13 00:15:31 -07:00
|
|
|
|
2023-10-24 14:54:00 -07:00
|
|
|
messages = [m for m in ast if type(m) is Message]
|
|
|
|
interfaces = [i for i in ast if type(i) is Interface]
|
|
|
|
|
|
|
|
jinja_env = Environment(loader=FileSystemLoader(pathlib.Path(__file__).parent.resolve()))
|
2023-10-13 15:04:25 -07:00
|
|
|
|
2023-10-24 14:54:00 -07:00
|
|
|
message_header_tmpl = jinja_env.get_template("message.h.jinja")
|
2023-10-13 15:04:25 -07:00
|
|
|
with open(filename + '.h', mode='w') as f:
|
2023-10-24 14:54:00 -07:00
|
|
|
message_header = message_header_tmpl.render(messages=messages)
|
|
|
|
f.write(message_header)
|
|
|
|
|
|
|
|
message_impl_tmpl = jinja_env.get_template("message.cpp.jinja")
|
|
|
|
message_impl_tmpl.globals['Type'] = Type
|
2023-10-13 15:04:25 -07:00
|
|
|
with open(filename + '.cpp', mode='w') as f:
|
2023-10-25 21:53:55 -07:00
|
|
|
message_impl = message_impl_tmpl.render(file=filebase, messages=messages)
|
2023-10-24 14:54:00 -07:00
|
|
|
f.write(message_impl)
|
|
|
|
|
2023-10-24 15:57:10 -07:00
|
|
|
client_header_tmpl = jinja_env.get_template("client.h.jinja")
|
|
|
|
with open(filename + '.client.h', mode='w') as f:
|
2023-10-25 21:53:55 -07:00
|
|
|
client_header = client_header_tmpl.render(file=filebase, interfaces=interfaces)
|
2023-10-24 15:57:10 -07:00
|
|
|
f.write(client_header)
|
|
|
|
|
|
|
|
client_impl_tmpl = jinja_env.get_template("client.cpp.jinja")
|
|
|
|
with open(filename + '.client.cpp', mode='w') as f:
|
2023-10-25 21:53:55 -07:00
|
|
|
client_impl = client_impl_tmpl.render(file=filebase, interfaces=interfaces)
|
2023-10-24 15:57:10 -07:00
|
|
|
f.write(client_impl)
|
|
|
|
|
|
|
|
server_header_tmpl = jinja_env.get_template("server.h.jinja")
|
|
|
|
with open(filename + '.server.h', mode='w') as f:
|
2023-10-25 21:53:55 -07:00
|
|
|
server_header = server_header_tmpl.render(file=filebase, interfaces=interfaces)
|
2023-10-24 15:57:10 -07:00
|
|
|
f.write(server_header)
|
|
|
|
|
|
|
|
server_impl_tmpl = jinja_env.get_template("server.cpp.jinja")
|
|
|
|
with open(filename + '.server.cpp', mode='w') as f:
|
2023-10-25 21:53:55 -07:00
|
|
|
server_impl = server_impl_tmpl.render(file=filebase, interfaces=interfaces)
|
2023-10-24 15:57:10 -07:00
|
|
|
f.write(server_impl)
|
2023-10-12 22:46:37 -07:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|