diff --git a/.github/workflows/compiler_tests.yml b/.github/workflows/compiler_tests.yml index f6becd86e..8dc339062 100644 --- a/.github/workflows/compiler_tests.yml +++ b/.github/workflows/compiler_tests.yml @@ -2,9 +2,9 @@ name: Compiler Tests on: push: - paths: ['src/', '.github/workflows/compiler_tests.yml'] + paths: ['src/**.v', '.github/workflows/compiler_tests.yml'] pull_request: - paths: ['src/', '.github/workflows/compiler_tests.yml'] + paths: ['src/**.v', '.github/workflows/compiler_tests.yml'] types: [opened, synchronize] jobs: diff --git a/src/builder/mod.v b/src/builder/mod.v deleted file mode 100644 index 2c07f664d..000000000 --- a/src/builder/mod.v +++ /dev/null @@ -1,22 +0,0 @@ -// 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 builder - -import prefs -import util - -pub struct Builder { -pub mut: - prefs &prefs.Prefs -} - -@[inline] -pub fn new(args []string) Builder { - return Builder{ - prefs: prefs.parse_args(args) or { util.error(err.msg()) } - } -} - -pub fn (mut b Builder) run() {} diff --git a/src/compiler/mod.v b/src/compiler/mod.v new file mode 100644 index 000000000..2aedbd1af --- /dev/null +++ b/src/compiler/mod.v @@ -0,0 +1,19 @@ +// 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 compiler + +import compiler.prefs +import compiler.report + +pub struct Compiler { +pub mut: + prefs &prefs.Prefs +} + +pub fn run(args []string) { + mut c := Compiler{ + prefs: prefs.parse_args(args) or { report.error(err.msg()) } + } +} diff --git a/src/prefs/mod.v b/src/compiler/prefs/mod.v similarity index 100% rename from src/prefs/mod.v rename to src/compiler/prefs/mod.v diff --git a/src/compiler/report/mod.v b/src/compiler/report/mod.v new file mode 100644 index 000000000..1d7d7f4f5 --- /dev/null +++ b/src/compiler/report/mod.v @@ -0,0 +1,53 @@ +// 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 report + +import term + +enum MsgLevel { + note + warn + error + ice +} + +@[inline] +fn format_msg(msg string, level MsgLevel) string { + return term.bold('rivetc: ') + match level { + .note { + '${term.bold(term.cyan('error:'))}' + } + .warn { + '${term.bold(term.yellow('error:'))}' + } + .error { + '${term.bold(term.red('error:'))}' + } + .ice { + '${term.bold(term.red('internal compiler error:'))}' + } + } + ' ${msg}' +} + +@[inline] +pub fn note(msg string) { + eprintln(format_msg(msg, .note)) +} + +@[inline] +pub fn warn(msg string) { + eprintln(format_msg(msg, .warn)) +} + +@[noreturn] +pub fn error(msg string) { + eprintln(format_msg(msg, .error)) + exit(101) +} + +@[noreturn] +pub fn ic_error(msg string) { + panic(format_msg(msg, .ice)) +} diff --git a/src/token/mod.v b/src/compiler/token/mod.v similarity index 99% rename from src/token/mod.v rename to src/compiler/token/mod.v index 289e58572..a2704105d 100644 --- a/src/token/mod.v +++ b/src/compiler/token/mod.v @@ -5,6 +5,7 @@ module token pub struct Pos { +pub: file string line_nr int // the line number in the source where the token occurred col int // the column in the source where the token occurred diff --git a/src/tokenizer/mod.v b/src/compiler/tokenizer/mod.v similarity index 93% rename from src/tokenizer/mod.v rename to src/compiler/tokenizer/mod.v index 44e707b09..5b4f72091 100644 --- a/src/tokenizer/mod.v +++ b/src/compiler/tokenizer/mod.v @@ -4,9 +4,9 @@ module tokenizer -import math -import prefs -import token +import compiler.prefs +import compiler.token +import compiler.util const lf = 10 const cr = 13 @@ -61,7 +61,7 @@ fn (t &Tokenizer) current_char() u8 { @[inline] fn (t &Tokenizer) current_pos() token.Pos { - return token.Pos{t.file, t.line, math.max(1, t.current_column()), self.pos} + return token.Pos{t.file, t.line, int_max(1, t.current_column()), self.pos} } @[inline] @@ -80,7 +80,7 @@ fn (mut t Tokenizer) eat_to_end_of_line() { } fn (mut t Tokenizer) inc_line_number() { - t.last_nl_pos = math.min(t.text.len - 1, t.pos) + t.last_nl_pos = int_min(t.text.len - 1, t.pos) if t.is_cr_lf { t.last_nl_pos++ } diff --git a/src/compiler/util/mod.v b/src/compiler/util/mod.v new file mode 100644 index 000000000..e3e5faa41 --- /dev/null +++ b/src/compiler/util/mod.v @@ -0,0 +1,31 @@ +// 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 util + +import os +import compiler.report + +pub fn read_file(path string) string { + return skip_bom(os.read_file(path) or { + // we use `ic_error` because this should not happen + report.ic_error(err.msg()) + }) +} + +pub fn skip_bom(file_content string) string { + mut raw_text := file_content + // BOM check + if raw_text.len >= 3 { + unsafe { + c_text := raw_text.str + if c_text[0] == 0xEF && c_text[1] == 0xBB && c_text[2] == 0xBF { + // skip three BOM bytes + offset_from_begin := 3 + raw_text = tos(c_text[offset_from_begin], vstrlen(c_text) - offset_from_begin) + } + } + } + return raw_text +} diff --git a/src/main.v b/src/main.v index 31e23d3db..5b461728a 100644 --- a/src/main.v +++ b/src/main.v @@ -5,9 +5,8 @@ module main import os -import builder +import compiler fn main() { - mut b := builder.new(os.args[1..]) - b.run() + compiler.run(os.args[1..]) } diff --git a/src/util/mod.v b/src/util/mod.v deleted file mode 100644 index 75b89590a..000000000 --- a/src/util/mod.v +++ /dev/null @@ -1,76 +0,0 @@ -// 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 util - -import term -import os - -enum MsgLevel { - note - warn - error - ice -} - -@[inline] -fn format_msg(msg string, level MsgLevel) string { - return match level { - .note { - '${term.bold('rivetc: ')}${term.bold(term.cyan('error:'))} ${msg}' - } - .warn { - '${term.bold('rivetc: ')}${term.bold(term.yellow('error:'))} ${msg}' - } - .error { - '${term.bold('rivetc: ')}${term.bold(term.red('error:'))} ${msg}' - } - .ice { - '${term.bold('rivetc: ')}${term.bold(term.red('internal compiler error:'))} ${msg}' - } - } -} - -pub fn note(msg string) { - eprintln(format_msg(msg, .note)) -} - -pub fn warn(msg string) { - eprintln(format_msg(msg, .warn)) -} - -@[noreturn] -pub fn error(msg string) { - eprintln(format_msg(msg, .error)) - exit(101) -} - -@[noreturn] -pub fn ic_error(msg string) { - panic(format_msg(msg, .ice)) -} - -pub fn read_file(path string) string { - return skip_bom(os.read_file(path) or { - // we use `ic_error` because this should not happen - ic_error(err.msg()) - '' - }) -} - -pub fn skip_bom(file_content string) string { - mut raw_text := file_content - // BOM check - if raw_text.len >= 3 { - unsafe { - c_text := raw_text.str - if c_text[0] == 0xEF && c_text[1] == 0xBB && c_text[2] == 0xBF { - // skip three BOM bytes - offset_from_begin := 3 - raw_text = tos(c_text[offset_from_begin], vstrlen(c_text) - offset_from_begin) - } - } - } - return raw_text -}