Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bootstrap 2 #37

Merged
merged 5 commits into from
Dec 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions bootstrap/makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
.ty/token.o:
ty obj src/token.ty
SRCS := $(wildcard src/*.ty)
OBJS := $(patsubst src/%.ty,.ty/%.o,$(SRCS))

all: $(OBJS)
echo $(OBJS)
echo $(SRCS)
touch .ty/all

$(OBJS): $(SRCS)
ty obj $<

.PHONY: clean
clean:
Expand Down
47 changes: 47 additions & 0 deletions bootstrap/src/lexer.ty
Original file line number Diff line number Diff line change
@@ -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
}
5 changes: 3 additions & 2 deletions bootstrap/src/token.ty
Original file line number Diff line number Diff line change
Expand Up @@ -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)
_ => {
Expand Down
28 changes: 23 additions & 5 deletions linter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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),
}
}
Expand Down Expand Up @@ -750,16 +752,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);
}
Expand Down Expand Up @@ -896,6 +904,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 {
Expand Down
1 change: 0 additions & 1 deletion parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Loading