Skip to content

Commit

Permalink
refactor(parser): rework Parser init
Browse files Browse the repository at this point in the history
  • Loading branch information
serkonda7 committed Dec 21, 2023
1 parent ab8671a commit 296eef6
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 21 deletions.
3 changes: 2 additions & 1 deletion cli/tools/ast/print_ast.bt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ fun main() {
return
}

file_ast := parser.parse(text, file, ast.new_table(), preference.Prefs{})
mut p := parser.new(ast.new_table(), preference.Prefs{})
file_ast := p.parse(text, file)
// TODO handle parser errors, warnings

println(file_ast)
Expand Down
12 changes: 6 additions & 6 deletions lib/bait/builder/builder.bt
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import bait.preference
import bait.util.timers

struct Builder {
table ast.Table
mut:
prefs preference.Prefs
parsed_files []ast.File
checker checker.Checker
parser parser.Parser
}

// Returns the absolute paths to all bait files in a directory that should be compiled
Expand All @@ -44,13 +44,13 @@ fun (b Builder) get_user_files(path string) []string {

fun (b Builder) parse_source_file(path string) ast.File {
text := os.read_file(path)
return parser.parse(text, path, b.table, b.prefs)
return b.parser.parse(text, path)
}

pub fun compile(prefs preference.Prefs) i32 {
mut b := Builder{
prefs = prefs
table = ast.new_table()
parser = parser.new(ast.new_table(), prefs)
}

// Tokenize and parse all specified files
Expand Down Expand Up @@ -155,7 +155,7 @@ pub fun compile(prefs preference.Prefs) i32 {
b.parsed_files = sorted_files
b.checker = checker.Checker{
prefs = b.prefs
table = b.table
table = b.parser.table
}
b.checker.check_files(sorted_files)
timers.show('CHECK')
Expand All @@ -177,7 +177,7 @@ pub fun compile(prefs preference.Prefs) i32 {
fun (b Builder) code_gen_js() i32 {
// Run JSGen and write it to the outfile
timers.start('GEN')
res := jsgen.gen(b.parsed_files, b.table, b.prefs) + '\n'
res := jsgen.gen(b.parsed_files, b.parser.table, b.prefs) + '\n'
timers.show('GEN')
ensure_dir_exists(os.dir(b.prefs.out_name))
os.write_file(b.prefs.out_name, res)
Expand All @@ -197,7 +197,7 @@ fun (b Builder) code_gen_js() i32 {

fun (mut b Builder) code_gen_c() i32 {
timers.start('GEN')
res := cgen.gen(b.parsed_files, b.table, b.prefs) + '\n'
res := cgen.gen(b.parsed_files, b.parser.table, b.prefs) + '\n'
timers.show('GEN')

if os.exists(b.prefs.out_name) and os.is_dir(b.prefs.out_name) {
Expand Down
6 changes: 3 additions & 3 deletions lib/bait/builder/redefined_fun_check.bt
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ fun (b Builder) check_redefined_functions() bool {
// Note: It is assumed that there are at most a few redefined functions.
// Thus the performance impact of looping over all files and root stmts is negligible.

if b.table.redefined_funs.length == 0 {
if b.parser.table.redefined_funs.length == 0 {
return false
}

// Remove duplicates that happen in case of a tripple redefinition
mut unique_redefs := []string
for name in b.table.redefined_funs {
for name in b.parser.table.redefined_funs {
if not unique_redefs.contains(name) {
unique_redefs.push(name)
}
Expand Down Expand Up @@ -57,7 +57,7 @@ fun (b Builder) check_redefined_functions() bool {
fun (b Builder) fun_signature(node ast.FunDecl) string {
mut sig := 'fun ${node.name}('
for i, p in node.params {
type_name := b.table.get_sym(p.typ).name
type_name := b.parser.table.get_sym(p.typ).name
sig += '${p.name} ${type_name}'
if i < node.params.length - 1 {
sig += ', '
Expand Down
1 change: 1 addition & 0 deletions lib/bait/lexer/lexer.bt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ mut:
pub fun (mut l Lexer) init(text string) {
l.text = text
l.line_starts = [0]
l.pos = 0
}

pub fun (l Lexer) get_pos() token.Pos {
Expand Down
28 changes: 17 additions & 11 deletions lib/bait/parser/parser.bt
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,9 @@ import bait.token

pub struct Parser {
pref preference.Prefs
path string
table ast.Table
lexer lexer.Lexer
mut:
path string
eofs i32
idx i32
tok token.Token
next_tok token.Token
val string
Expand All @@ -28,23 +25,32 @@ mut:
is_struct_possible bool
should_abort bool
pub mut:
table ast.Table
lexer lexer.Lexer
warnings []errors.Message
errors []errors.Message
}

pub fun parse(text string, path string, table ast.Table, pref preference.Prefs) ast.File {
mut p := Parser{
lexer = lexer.Lexer{}
pref = pref
path = path
pub fun new(table ast.Table, pref preference.Prefs) Parser {
return Parser{
table = table
is_struct_possible = true
pref = pref
}
}

pub fun (mut p Parser) parse(text string, path string) ast.File {
// Init Parser
p.lexer.init(text)

p.path = path
p.eofs = 0
p.import_aliases = map[string]string
p.expr_pkg = ''
p.is_for_init = false
p.is_struct_possible = true
p.should_abort = false
p.next()

// Start Parsing
pkg_decl := p.package_decl()

// Validate the files package name actually matches the desired import
Expand Down

0 comments on commit 296eef6

Please sign in to comment.