Skip to content

Commit

Permalink
parser (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
StunxFS committed Nov 25, 2024
1 parent eb539bd commit 0babaa5
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/compiler/ast/SourceFile.v
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?
}
52 changes: 52 additions & 0 deletions src/compiler/parser/mod.v
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()
}
}

0 comments on commit 0babaa5

Please sign in to comment.