-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright (C) 2024-present Jose Mendoza - All rights reserved. Use of this | ||
// source code is governed by an MIT license that can be found in the LICENSE | ||
// file. | ||
|
||
module ast | ||
|
||
import compiler.util | ||
|
||
@[heap] | ||
pub struct SourceFile { | ||
pub: | ||
file string | ||
content string | ||
pub mut: | ||
lines ?[]string | ||
} | ||
|
||
pub fn SourceFile.new(file string) &SourceFile { | ||
content := util.read_file(file) | ||
return &SourceFile{ | ||
file: file | ||
content: content | ||
} | ||
} | ||
|
||
pub fn SourceFile.from_memory(content string) &SourceFile { | ||
return &SourceFile{ | ||
file: '<memory>' | ||
content: content | ||
} | ||
} | ||
|
||
pub fn (mut sf SourceFile) get_lines() []string { | ||
if sf.lines != none { | ||
return sf.lines | ||
} | ||
sf.lines = sf.content.split_into_lines() | ||
return sf.lines? | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright (C) 2024-present Jose Mendoza - All rights reserved. Use of this | ||
// source code is governed by an MIT license that can be found in the LICENSE | ||
// file. | ||
|
||
module parser | ||
|
||
import compiler.ast | ||
import compiler.token | ||
import compiler.context | ||
import compiler.tokenizer | ||
|
||
pub struct Parser { | ||
mut: | ||
ctx &context.CContext | ||
|
||
source_file &ast.SourceFile = unsafe { nil } | ||
|
||
tokenizer tokenizer.Tokenizer | ||
prev_tok token.Token | ||
tok token.Token | ||
peek_tok token.Token | ||
} | ||
|
||
pub fn new(ctx &context.CContext) &Parser { | ||
return &Parser{ | ||
ctx: ctx | ||
} | ||
} | ||
|
||
pub fn (mut p Parser) parse() { | ||
p.parse_file(p.ctx.options.input) | ||
} | ||
|
||
fn (mut p Parser) parse_file(file string) { | ||
p.source_file = ast.SourceFile.new(file) | ||
p.tokenizer = tokenizer.from_source_file(p.ctx, p.source_file) | ||
p.advance(3) | ||
|
||
p.ctx.source_files << p.source_file | ||
} | ||
|
||
fn (mut p Parser) next() { | ||
p.prev_tok = p.tok | ||
p.tok = p.peek_tok | ||
p.peek_tok = p.tokenizer.next() | ||
} | ||
|
||
fn (mut p Parser) advance(n int) { | ||
for _ in 0 .. n { | ||
p.next() | ||
} | ||
} |