Skip to content

Commit

Permalink
generate lua files in a directory called dist
Browse files Browse the repository at this point in the history
  • Loading branch information
StunxFS committed Jun 7, 2024
1 parent 3850002 commit e603125
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__pycache__
*.lua
dist/
4 changes: 2 additions & 2 deletions bsc/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

import os, sys

SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.dirname(SCRIPT_DIR))
BSC_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.dirname(BSC_DIR))

from bsc import Context

Expand Down
4 changes: 2 additions & 2 deletions bsc/astgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ def fn_decl(self, *nodes):

def fn_args(self, *nodes):
is_method = str(nodes[0]) == "self"
return (is_method, nodes[2:] if is_method else nodes)
return (is_method, nodes[1:] if is_method else nodes)

def fn_arg(self, *nodes):
return FnArg(nodes[1], nodes[3], nodes[-1])
return FnArg(nodes[0].name, nodes[2], nodes[-1])

def fn_body(self, *nodes):
stmts = []
Expand Down
5 changes: 4 additions & 1 deletion bsc/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,8 @@ def gen_decl(self, decl):
self.gen_fn_decl(decl)

def gen_fn_decl(self, decl):
luafn = LuaFunction(decl.name, [])
args = []
for arg in decl.args:
args.append(LuaIdent(arg.name))
luafn = LuaFunction(decl.name, args)
self.cur_module.decls.append(luafn)
38 changes: 19 additions & 19 deletions bsc/grammar.lark
Original file line number Diff line number Diff line change
Expand Up @@ -189,23 +189,23 @@ OP_BIT_AND_ASSIGN: "&="
OP_BIT_OR_ASSIGN: "|="
OP_BIT_XOR_ASSIGN: "^="

KW_AS: "as"
KW_CLASS: "class"
KW_CONST: "const"
KW_ELSE: "else"
KW_EXTERN: "extern"
KW_FALSE: "false"
KW_FN: "fn"
KW_IF: "if"
KW_MATCH: "match"
KW_MOD: "mod"
KW_NIL: "nil"
KW_PKG: "pkg"
KW_PROT: "prot"
KW_PUB: "pub"
KW_SELF: "self"
KW_TRUE: "true"
KW_USE: "use"
KW_VAR: "var"
KW_WHILE: "while"
KW_AS.10: "as"
KW_CLASS.10: "class"
KW_CONST.10: "const"
KW_ELSE.10: "else"
KW_EXTERN.10: "extern"
KW_FALSE.10: "false"
KW_FN.10: "fn"
KW_IF.10: "if"
KW_MATCH.10: "match"
KW_MOD.10: "mod"
KW_NIL.10: "nil"
KW_PKG.10: "pkg"
KW_PROT.10: "prot"
KW_PUB.10: "pub"
KW_SELF.10: "self"
KW_TRUE.10: "true"
KW_USE.10: "use"
KW_VAR.10: "var"
KW_WHILE.10: "while"

4 changes: 4 additions & 0 deletions bsc/lua_ast/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ def __init__(self, name):
self.name = name
self.decls = []

class LuaIdent:
def __init__(self, name):
self.name = name

class LuaFunction:
def __init__(self, name, args):
self.name = name
Expand Down
21 changes: 16 additions & 5 deletions bsc/lua_ast/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# source code is governed by an MIT license that can be found in the
# LICENSE file.

import os
from bsc import utils
from bsc.lua_ast import *

Expand All @@ -13,20 +14,25 @@ def __init__(self, ctx, modules):
self.lua_file = utils.Builder()

def render_modules(self):
if not os.path.exists("dist"):
os.mkdir("dist/")
for module in self.modules:
self.cur_module = module
self.render_module(module)

def render_module(self, module):
self.lua_file.writeln("-- Autogenerated with the BlueScript compiler")
self.lua_file.writeln(f"-- {utils.full_version()}")
self.lua_file.writeln("-- DO NOT MODIFY MANUALLY!\n")
self.lua_file.writeln(
f"-- Autogenerated by the BlueScript compiler - {utils.full_version()}"
)
self.lua_file.writeln(
"-- WARNING: DO NOT MODIFY MANUALLY! YOUR CHANGES WILL BE OVERWRITTEN --\n"
)

self.lua_file.writeln(f"local {module.name} = {{}}\n")
self.render_decls(module.decls)
self.lua_file.writeln(f"return {module.name}")

with open(f"{module.name}.lua", "w") as f:
with open(f"dist/{module.name}.lua", "w") as f:
f.write(str(self.lua_file))
self.lua_file.clear()

Expand All @@ -39,5 +45,10 @@ def render_decl(self, decl):
self.render_fn_decl(decl)

def render_fn_decl(self, decl):
self.lua_file.writeln(f"function {self.cur_module.name}.{decl.name}()")
self.lua_file.write(f"function {self.cur_module.name}.{decl.name}(")
for i, arg in enumerate(decl.args):
self.lua_file.write(arg.name)
if i < len(decl.args) - 1:
self.lua_file.write(", ")
self.lua_file.writeln(")")
self.lua_file.writeln("end\n")
8 changes: 0 additions & 8 deletions examples/gentest.bs

This file was deleted.

2 changes: 1 addition & 1 deletion examples/sema.bs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ pub fn main() {
print("hello")
}

fn my_fn(){}
fn my_fn(a: number){}

0 comments on commit e603125

Please sign in to comment.