Skip to content

Commit

Permalink
lex: fix range comment lexing and reimplement the File
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Mar 22, 2024
1 parent 37dbf00 commit 7b6b0fd
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 40 deletions.
4 changes: 3 additions & 1 deletion src/julec/main.jule
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ static HELP_MAP: [...][2]str = [
[CMD_MOD, "Module management"],
]

fn print_error_message(msg: str) { outln(msg) }
fn print_error_message(msg: str) {
outln(msg)
}

// Command: julec help
fn help(&args: []str) {
Expand Down
2 changes: 1 addition & 1 deletion src/julec/obj/cxx/object.jule
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl ObjectCoder {

// Returns location information of token as cstr bytes.
fn loc_info(self, &t: Token): str {
let mut loc = t.file.path()
let mut loc = t.file.path

// Normalize path if production compilation enabled.
if env::PRODUCTION {
Expand Down
2 changes: 1 addition & 1 deletion src/julec/optimizing/scope.jule
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl ScopeOptimizer {
let mut m = (&FnCallExprModel)(d.model)
if env::PRODUCTION {
if !m.func.is_builtin() &&
is_std_package(m.func.decl.token.file.path(), "debug") {
is_std_package(m.func.decl.token.file.path, "debug") {
self.set_current_stmt(nil)
ret
}
Expand Down
24 changes: 18 additions & 6 deletions std/jule/ast/node.jule
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,9 @@ pub struct LitExpr {

impl LitExpr {
// Reports whether literal is nil value.
pub fn is_nil(self): bool { ret self.value == TokenKind.Nil }
pub fn is_nil(self): bool {
ret self.value == TokenKind.Nil
}
}

// Unsafe expression.
Expand Down Expand Up @@ -307,7 +309,9 @@ pub struct FieldExprPair {

impl FieldExprPair {
// Reports whether pair targeted field.
pub fn is_targeted(self): bool { ret self.field.id != TokenId.Na }
pub fn is_targeted(self): bool {
ret self.field.id != TokenId.Na
}
}

// Struct literal instiating expression.
Expand All @@ -324,7 +328,9 @@ pub struct BraceLit {

impl BraceLit {
// Reports whether literal is empty ( {} ).
pub fn is_empty(self): bool { ret self.exprs.len == 0 }
pub fn is_empty(self): bool {
ret self.exprs.len == 0
}
}

// Key-value pair expression.
Expand All @@ -343,7 +349,9 @@ pub struct SliceExpr {

impl SliceExpr {
// Reports whether slice is empty.
pub fn is_empty(self): bool { ret self.elems.len == 0 }
pub fn is_empty(self): bool {
ret self.elems.len == 0
}
}

// Indexing expression.
Expand Down Expand Up @@ -459,7 +467,9 @@ pub struct FnDecl {

impl FnDecl {
// Reports whether function is anonymous.
pub fn is_anon(self): bool { ret self.ident == Ident.Anon }
pub fn is_anon(self): bool {
ret self.ident == Ident.Anon
}
}

// Variable declaration.
Expand Down Expand Up @@ -508,7 +518,9 @@ pub struct WhileKind {

impl WhileKind {
// Reports whether kind is while-next iteration.
pub fn is_while_next(self): bool { ret self.next != nil }
pub fn is_while_next(self): bool {
ret self.next != nil
}
}

// Range iteration kind.
Expand Down
2 changes: 1 addition & 1 deletion std/jule/importer/directive_eval.jule
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl DirectiveEval {
kind: LogKind.Error,
row: t.row,
column: t.column,
path: t.file.path(),
path: t.file.path,
text: logf(fmt, args...),
})
}
Expand Down
2 changes: 1 addition & 1 deletion std/jule/importer/importer.jule
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl Importer for JuleImporter {

let _path = join(path, dirent.name)
let mut file = new_file_set(_path)
file.fill(read_buff(file.path()))
file.fill(read_buff(file.path))
let mut errors = lex(file, LexMode.Standard)
if errors.len > 0 {
ret nil, errors
Expand Down
35 changes: 17 additions & 18 deletions std/jule/lex/buffer.jule → std/jule/lex/file.jule
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,37 @@ use path for std::fs::path

// Fileset for lexing.
pub struct File {
pub data: []byte

_path: str
mut _tokens: []Token
pub path: str
pub data: []byte
pub tokens: []Token
}

impl File {
// Reports whether file path is exist and accessible.
pub fn is_ok(self): bool {
Status.of(self._path) else { ret false }
_ = Status.of(self.path) else { ret false }
ret true
}

// Fill data.
pub fn fill(mut self, mut data: []byte) { self.data = data }

// Returns path.
pub fn path(self): str { ret self._path }
pub fn fill(mut self, mut data: []byte) {
self.data = data
}

// Returns directory of file's path.
pub fn dir(self): str { ret path::dir(self._path) }
pub fn dir(self): str {
ret path::dir(self.path)
}

// Returns filename.
pub fn name(self): str { ret path::base(self._path) }
pub fn name(self): str {
ret path::base(self.path)
}

// Returns self as uintptr.
pub fn addr(self): uintptr { ret uintptr(&self) }

// Returns tokens of file.
// Tokens are mutable.
pub fn tokens(mut self): []Token { ret self._tokens }
pub fn addr(self): uintptr {
ret uintptr(&self)
}

// Returns line (not include new-line char) by row.
// Returns empty string if line is not buffer.
Expand All @@ -49,7 +49,6 @@ impl File {
if self.data[i] != '\n' {
continue
}

n++
if n == row {
ret str(self.data[line_start:i])
Expand All @@ -63,6 +62,6 @@ impl File {
// Returns new File points to Jule file.
pub fn new_file_set(path: str): &File {
ret &File{
_path: path,
path: path,
}
}
8 changes: 4 additions & 4 deletions std/jule/lex/lex.jule
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ fn make_err(row: int, col: int, &f: &File, fmt: LogMsg, args: ...any): Log {
kind: LogKind.Error,
row: row,
column: col,
path: f.path(),
path: f.path,
text: logf(fmt, args...),
}
}
Expand Down Expand Up @@ -562,12 +562,12 @@ impl Lex {
self.column += 1
if self.pos+1 < self.file.data.len && r == '*' &&
self.file.data[self.pos+1] == '/' {
self.column += 2
self.pos += 2
if self.mode & LexMode.Comment == LexMode.Comment {
token.id = TokenId.Comment
token.kind = str(self.file.data[start:self.pos])
}
self.column += 2
self.pos += 2
ret
}
}
Expand Down Expand Up @@ -848,6 +848,6 @@ pub fn lex(mut f: &File, mode: LexMode): []Log {
ret lex.errors
}

f._tokens = lex.tokens
f.tokens = lex.tokens
ret nil
}
9 changes: 4 additions & 5 deletions std/jule/parser/parser.jule
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn make_err(row: int, col: int, &f: &File, fmt: LogMsg, args: ...any): Log {
kind: LogKind.Error,
row: row,
column: col,
path: f.path(),
path: f.path,
text: logf(fmt, args...),
}
}
Expand Down Expand Up @@ -196,7 +196,7 @@ fn compiler_err(&token: Token, &fmt: LogMsg, args: ...any): Log {
kind: LogKind.Error,
row: token.row,
column: token.column,
path: token.file.path(),
path: token.file.path,
text: logf(fmt, args...),
line: token.file.get_row(token.row),
}
Expand Down Expand Up @@ -1929,13 +1929,12 @@ impl Parser {
file: f,
}

let mut tokens = f.tokens()
self.check_ranges(tokens)
self.check_ranges(f.tokens)
if self.errors.len > 0 {
ret
}

let mut stmts = split_stmts(tokens)
let mut stmts = split_stmts(f.tokens)

// Get top directives.
let mut i = 0
Expand Down
4 changes: 2 additions & 2 deletions std/jule/sema/sema.jule
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fn compiler_err(&token: Token, line: bool, fmt: LogMsg, args: ...any): Log {
kind: LogKind.Error,
row: token.row,
column: token.column,
path: token.file.path(),
path: token.file.path,
text: logf(fmt, args...),
}
if line {
Expand Down Expand Up @@ -2305,7 +2305,7 @@ impl Sema {
}

let s = sptr.elem.strct()
if s == nil || !is_std_package(s.decl.token.file.path(), "testing") {
if s == nil || !is_std_package(s.decl.token.file.path, "testing") {
self.push_err(f.decl.token, LogMsg.WrongTestFnDecl)
self.push_suggestion(LogMsg.UseExpectedTestFnDecl)
ret
Expand Down

0 comments on commit 7b6b0fd

Please sign in to comment.