Skip to content

Commit

Permalink
rearrange source
Browse files Browse the repository at this point in the history
  • Loading branch information
StunxFS committed Nov 19, 2024
1 parent 29c5412 commit 98577b2
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 108 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/compiler_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
22 changes: 0 additions & 22 deletions src/builder/mod.v

This file was deleted.

19 changes: 19 additions & 0 deletions src/compiler/mod.v
Original file line number Diff line number Diff line change
@@ -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{

Check warning on line 16 in src/compiler/mod.v

View workflow job for this annotation

GitHub Actions / ubuntu-gcc

unused variable: `c`
prefs: prefs.parse_args(args) or { report.error(err.msg()) }
}
}
File renamed without changes.
53 changes: 53 additions & 0 deletions src/compiler/report/mod.v
Original file line number Diff line number Diff line change
@@ -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))
}
1 change: 1 addition & 0 deletions src/token/mod.v → src/compiler/token/mod.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions src/tokenizer/mod.v → src/compiler/tokenizer/mod.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand All @@ -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++
}
Expand Down
31 changes: 31 additions & 0 deletions src/compiler/util/mod.v
Original file line number Diff line number Diff line change
@@ -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
}
5 changes: 2 additions & 3 deletions src/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -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..])
}
76 changes: 0 additions & 76 deletions src/util/mod.v

This file was deleted.

0 comments on commit 98577b2

Please sign in to comment.