From 00be727b3e4bd89c9e12fe0c993a0646a6b1fae3 Mon Sep 17 00:00:00 2001 From: Chris Date: Thu, 26 Dec 2024 20:35:08 -0600 Subject: [PATCH 1/3] took a quick look at how the lexer would be implemented --- bootstrap/makefile | 3 +++ bootstrap/src/lexer.ty | 47 ++++++++++++++++++++++++++++++++++++++++++ bootstrap/src/token.ty | 5 +++-- linter/src/lib.rs | 32 +++++++++++++++++++++++----- parser/src/lib.rs | 1 - types/src/lib.rs | 4 ++-- 6 files changed, 82 insertions(+), 10 deletions(-) create mode 100644 bootstrap/src/lexer.ty diff --git a/bootstrap/makefile b/bootstrap/makefile index 17e328d..249ad65 100644 --- a/bootstrap/makefile +++ b/bootstrap/makefile @@ -1,3 +1,6 @@ +.ty/lexer.o: + ty obj src/lexer.ty + .ty/token.o: ty obj src/token.ty diff --git a/bootstrap/src/lexer.ty b/bootstrap/src/lexer.ty new file mode 100644 index 0000000..f2a6b7a --- /dev/null +++ b/bootstrap/src/lexer.ty @@ -0,0 +1,47 @@ +const Token = import "token.Token" +pub const TokenError = import "token.TokenError" +const get_next = import "token.get_next" + +pub type Lexeme = struct { + token: Token, + slice: [char], + start: u32, + end: u32 +} + +pub type Lexer = struct { + current: ?Lexeme, + buffer: &[char], + idx: u32, +} + +pub const new = fn(buffer: &[char]) Lexer { + return Lexer { + buffer: buffer, + current : undefined, + idx: 0 + } +} + +pub const peek = fn(self: *Lexer) TokenError!?Lexeme { + if (self.current) { + return clone self.current + } + let len = 0 + let end = self.buf.len() + let result = try get_next(self.buf[self.idx..end], *len)? + self.current = Lexeme { + token: result, + slice: clone self.buf[self.idx..len], + start: self.idx, + end: len + } + self.idx += len + return clone self.current +} + +pub const collect = fn(self: *Lexer) Lexeme { + let temp = clone self.current.unwrap() + self.current = undefined + return temp +} diff --git a/bootstrap/src/token.ty b/bootstrap/src/token.ty index 4115518..4b80147 100644 --- a/bootstrap/src/token.ty +++ b/bootstrap/src/token.ty @@ -302,10 +302,11 @@ const build_string = fn(buf: &[char], len: *u32, end: char) TokenError!Token { } return Token.String } -pub const get_next = fn(buf: &[char], len: *u32) TokenError!Token { + +pub const get_next = fn(buf: &[char], len: *u32) TokenError!?Token { len = 0 const c = buf[0] - return match (c) { + return match (c?) { c.isAlphabetic() => tokenize_chars(buf, len) c.isDigit() => tokenize_num(buf, len) _ => { diff --git a/linter/src/lib.rs b/linter/src/lib.rs index c78a7b1..ae1568e 100644 --- a/linter/src/lib.rs +++ b/linter/src/lib.rs @@ -77,6 +77,7 @@ impl<'buf, 'ttb, 'sco> LintSource<'buf, 'ttb, 'sco> { Expr::UnOp(un) => match un.op.token { Token::Dash => self.check_negate(un), Token::Exclam => self.check_not(un), + Token::Try => self.check_try(un), Token::Ampersand => self.check_borrow_ro(un), Token::Asterisk => self.check_borrow_mut(un), Token::Copy => self.check_copy(un), @@ -112,6 +113,7 @@ impl<'buf, 'ttb, 'sco> LintSource<'buf, 'ttb, 'sco> { Expr::ArgDef(arg) => self.check_arg_def(&arg), Expr::ArrayType(arr) => self.check_array_type(&arr), Expr::ArrayAccess(arr) => self.check_array_access(&arr), + Expr::UndefBubble(u) => self.check_undefined_bubble(&u), _ => panic!("type-lang linter issue, expr not implemented {:?}", to_cmp), } } @@ -257,6 +259,7 @@ impl<'buf, 'ttb, 'sco> LintSource<'buf, 'ttb, 'sco> { pub fn check_symbol_ref(&mut self, symbol: &Symbol) -> ResultTreeType { let ss = self.scopes.get(self.curr_scope as usize).unwrap(); + println!("check symbol = {:?}", symbol); let tt = ss .get_tt_same_up(&symbol.val.slice, self.ttbls, self.scopes) .unwrap(); @@ -459,6 +462,7 @@ impl<'buf, 'ttb, 'sco> LintSource<'buf, 'ttb, 'sco> { } pub fn check_prop_init(&mut self, prop: &PropAssignment) -> ResultTreeType { + println!("prop init = {:?}", prop); let result = self.lint_recurse(&prop.val)?; let decl = self.lint_recurse(&prop.ident)?; let slice = decl.0.into_symbol_init().ident.clone(); @@ -477,6 +481,7 @@ impl<'buf, 'ttb, 'sco> LintSource<'buf, 'ttb, 'sco> { pub fn check_props_init(&mut self, props: &PropAssignments) -> ResultTreeType { let prev = self.lint_recurse(&props.prev)?; + println!("prev = {:?}", prev); let current_scope = self.curr_scope; let scope = self.get_tt_by_symbol(&prev.0.into_symbol_access().ident); let temp_scope = scope.into_child_scope(); @@ -485,6 +490,7 @@ impl<'buf, 'ttb, 'sco> LintSource<'buf, 'ttb, 'sco> { if let Some(p) = &props.props { let result: Vec = p.into_iter().map(|e| self.lint_recurse(&e)).collect(); + println!("RESULT = {:?}", result); let mut struct_init = StructInitialize { idents: vec![], vals: vec![], @@ -750,16 +756,22 @@ impl<'buf, 'ttb, 'sco> LintSource<'buf, 'ttb, 'sco> { return ok_tree!(Copy, unop, curried); } + pub fn check_undefined_bubble(&mut self, un: &UndefBubble) -> ResultTreeType { + let result = self.lint_recurse(&un.prev)?; + let unop = UnaryOp { + val: result.0, + curried: result.1, + }; + let curried = unop.curried.clone(); + return ok_tree!(BubbleUndef, unop, curried); + } + pub fn check_clone(&mut self, un: &UnOp) -> ResultTreeType { let result = self.lint_recurse(&un.val)?; - let mut unop = UnaryOp { + let unop = UnaryOp { val: result.0, curried: result.1, }; - match unop.val.as_ref().as_ref() { - TypeTree::BoolValue(_) => unop.curried = Ty::Bool, - _ => panic!("clone check failed"), - } let curried = unop.curried.clone(); return ok_tree!(Clone, unop, curried); } @@ -896,6 +908,16 @@ impl<'buf, 'ttb, 'sco> LintSource<'buf, 'ttb, 'sco> { return ok_tree!(NotEq, binop, curried); } + pub fn check_try(&mut self, un: &UnOp) -> ResultTreeType { + let result = self.lint_recurse(&un.val)?; + let unop = UnaryOp { + val: result.0, + curried: result.1, + }; + let curried = unop.curried.clone(); + return ok_tree!(BubbleError, unop, curried); + } + pub fn check_not(&mut self, un: &UnOp) -> ResultTreeType { let result = self.lint_recurse(&un.val)?; let mut unop = UnaryOp { diff --git a/parser/src/lib.rs b/parser/src/lib.rs index 2ac90fc..72453b1 100644 --- a/parser/src/lib.rs +++ b/parser/src/lib.rs @@ -745,7 +745,6 @@ impl<'s> Parser<'s> { ]) { match x.token { Token::Question => self.resolve_access(expr!(UndefBubble, prev)), - Token::Try => self.resolve_access(expr!(ErrBubble, prev)), Token::Period => { let ident = self .ident() diff --git a/types/src/lib.rs b/types/src/lib.rs index b0cf2a0..21529f8 100644 --- a/types/src/lib.rs +++ b/types/src/lib.rs @@ -230,9 +230,9 @@ pub enum TypeTree { Range(BinaryOp), CastAs(BinaryOp), Gt(BinaryOp), - BubbleUndef(BinaryOp), - BubbleError(BinaryOp), // unops + BubbleUndef(UnaryOp), + BubbleError(UnaryOp), ReadBorrow(UnaryOp), MutBorrow(UnaryOp), Copy(UnaryOp), From 67c45e3d5d5ec96f4d79731c79575664c56555c0 Mon Sep 17 00:00:00 2001 From: Chris Date: Sat, 28 Dec 2024 12:42:00 -0600 Subject: [PATCH 2/3] makefile --- bootstrap/makefile | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bootstrap/makefile b/bootstrap/makefile index 46ad908..1cf6bea 100644 --- a/bootstrap/makefile +++ b/bootstrap/makefile @@ -1,3 +1,6 @@ +SRCS := $(src/%.ty) +OBJS := $(patsubst src/%.ty,.ty/%.o,$(SRCS)) + .ty/lexer.o: ty obj src/lexer.ty From 91f8175ed2c243125407f82c4509a36f0da6f848 Mon Sep 17 00:00:00 2001 From: Chris Date: Sat, 28 Dec 2024 20:57:41 -0600 Subject: [PATCH 3/3] bootstrap 2 failure. lots to do before --- bootstrap/makefile | 12 +++++++----- linter/src/lib.rs | 4 ---- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/bootstrap/makefile b/bootstrap/makefile index 1cf6bea..952794a 100644 --- a/bootstrap/makefile +++ b/bootstrap/makefile @@ -1,11 +1,13 @@ -SRCS := $(src/%.ty) +SRCS := $(wildcard src/*.ty) OBJS := $(patsubst src/%.ty,.ty/%.o,$(SRCS)) -.ty/lexer.o: - ty obj src/lexer.ty +all: $(OBJS) + echo $(OBJS) + echo $(SRCS) + touch .ty/all -.ty/token.o: - ty obj src/token.ty +$(OBJS): $(SRCS) + ty obj $< .PHONY: clean clean: diff --git a/linter/src/lib.rs b/linter/src/lib.rs index cf5e5c2..eb454ff 100644 --- a/linter/src/lib.rs +++ b/linter/src/lib.rs @@ -259,7 +259,6 @@ impl<'buf, 'ttb, 'sco> LintSource<'buf, 'ttb, 'sco> { pub fn check_symbol_ref(&mut self, symbol: &Symbol) -> ResultTreeType { let ss = self.scopes.get(self.curr_scope as usize).unwrap(); - println!("check symbol = {:?}", symbol); let tt = ss .get_tt_same_up(&symbol.val.slice, self.ttbls, self.scopes) .unwrap(); @@ -462,7 +461,6 @@ impl<'buf, 'ttb, 'sco> LintSource<'buf, 'ttb, 'sco> { } pub fn check_prop_init(&mut self, prop: &PropAssignment) -> ResultTreeType { - println!("prop init = {:?}", prop); let result = self.lint_recurse(&prop.val)?; let decl = self.lint_recurse(&prop.ident)?; let slice = decl.0.into_symbol_init().ident.clone(); @@ -481,7 +479,6 @@ impl<'buf, 'ttb, 'sco> LintSource<'buf, 'ttb, 'sco> { pub fn check_props_init(&mut self, props: &PropAssignments) -> ResultTreeType { let prev = self.lint_recurse(&props.prev)?; - println!("prev = {:?}", prev); let current_scope = self.curr_scope; let scope = self.get_tt_by_symbol(&prev.0.into_symbol_access().ident); let temp_scope = scope.into_child_scope(); @@ -490,7 +487,6 @@ impl<'buf, 'ttb, 'sco> LintSource<'buf, 'ttb, 'sco> { if let Some(p) = &props.props { let result: Vec = p.into_iter().map(|e| self.lint_recurse(&e)).collect(); - println!("RESULT = {:?}", result); let mut struct_init = StructInitialize { idents: vec![], vals: vec![],