Skip to content

Commit

Permalink
code context
Browse files Browse the repository at this point in the history
  • Loading branch information
StunxFS committed Nov 19, 2024
1 parent 98577b2 commit 420a085
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 24 deletions.
10 changes: 10 additions & 0 deletions src/compiler/context/mod.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// 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 context

pub struct CContext {
pub mut:
options &Options
}
20 changes: 10 additions & 10 deletions src/compiler/prefs/mod.v → src/compiler/context/options.v
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// source code is governed by an MIT license that can be found in the LICENSE
// file.

module prefs
module context

import os
import flag
Expand All @@ -11,36 +11,36 @@ import flag
@[xdoc: 'The Rivet programming language compiler']
@[name: 'rivetc']
@[version: '0.1.0']
pub struct Prefs {
pub struct Options {
mut:
input string @[ignore]

show_help bool @[long: help; short: h; xdoc: 'Print help information.']
}

@[inline]
pub fn parse_args(args []string) !&Prefs {
mut prefs, remaining := flag.to_struct[Prefs](args)!
pub fn parse_args(args []string) !&Options {
mut options, remaining := flag.to_struct[Options](args)!

if prefs.show_help {
eprintln(flag.to_doc[Prefs]()!)
if options.show_help {
eprintln(flag.to_doc[Options]()!)
exit(0)
}

if remaining.len == 1 {
input := remaining[0]
match true {
os.is_file(input) {
prefs.input = input
options.input = input
}
os.is_dir(input) {
mut main_ri := os.join_path(input, 'main.ri')
if os.exists(main_ri) {
prefs.input = main_ri
options.input = main_ri
} else {
main_ri = os.join_path(input, 'src', 'main.ri')
if os.exists(main_ri) {
prefs.input = main_ri
options.input = main_ri
} else {
return error('`${input}` is not a valid input, no file `${main_ri}` found')
}
Expand All @@ -56,5 +56,5 @@ pub fn parse_args(args []string) !&Prefs {
return error('only one input is expected')
}

return &prefs
return &options
}
11 changes: 3 additions & 8 deletions src/compiler/mod.v
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,11 @@

module compiler

import compiler.prefs
import compiler.context
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()) }
mut c_ctx := &context.CContext{

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

View workflow job for this annotation

GitHub Actions / ubuntu-gcc

unused variable: `c_ctx`
options: context.parse_args(args) or { report.error(err.msg()) }
}
}
12 changes: 6 additions & 6 deletions src/compiler/tokenizer/mod.v
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

module tokenizer

import compiler.prefs
import compiler.context
import compiler.token
import compiler.util

Expand All @@ -19,8 +19,8 @@ fn is_new_line(ch u8) bool {

@[minify]
pub struct Tokenizer {
prefs &prefs.Prefs
text string
ctx &context.CContext
text string
mut:
file string
line int = -1
Expand All @@ -34,10 +34,10 @@ mut:
tidx int = -1
}

pub fn from_file(prefs_ &prefs.Prefs, path string) &Tokenizer {
pub fn from_file(options_ &options.Options, path string) &Tokenizer {
mut t := &Tokenizer{
prefs: unsafe { prefs_ }
text: util.read_file(path)
options: unsafe { options_ }
text: util.read_file(path)
}
t.file = path
t.tokenize_remaining_text()
Expand Down

0 comments on commit 420a085

Please sign in to comment.