mirror of
https://github.com/azur1s/bobbylisp.git
synced 2024-10-16 02:37:40 -05:00
fix tuple codegen, generics example, change syntax
This commit is contained in:
parent
9c3a390ae8
commit
3768562c52
8
Cargo.lock
generated
8
Cargo.lock
generated
|
@ -28,6 +28,13 @@ version = "1.0.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "checker"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "chumsky"
|
||||
version = "0.8.0"
|
||||
|
@ -107,6 +114,7 @@ dependencies = [
|
|||
name = "hzc"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"checker",
|
||||
"codegen",
|
||||
"colored",
|
||||
"syntax",
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
members = [
|
||||
"core",
|
||||
"syntax",
|
||||
"checker",
|
||||
"transformer",
|
||||
"codegen",
|
||||
]
|
7
checker/Cargo.toml
Normal file
7
checker/Cargo.toml
Normal file
|
@ -0,0 +1,7 @@
|
|||
[package]
|
||||
name = "checker"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
syntax = { path = "../syntax" }
|
1
checker/src/lib.rs
Normal file
1
checker/src/lib.rs
Normal file
|
@ -0,0 +1 @@
|
|||
pub mod syntax;
|
0
checker/src/syntax.rs
Normal file
0
checker/src/syntax.rs
Normal file
|
@ -34,8 +34,7 @@ impl Codegen {
|
|||
match expr {
|
||||
Expr::Literal(lit) => self.gen_literal(&lit.0),
|
||||
Expr::Identifier(name) => { format!("_{}{}", name.0, semicolon!()) },
|
||||
Expr::Tuple(elems) => { format!("({}{})", elems.iter().map(|e| self.gen_expr(&e.0, false)).collect::<Vec<_>>().join(", "), semicolon!()) },
|
||||
Expr::Vector(elems) => { format!("[{}{}]", elems.iter().map(|e| self.gen_expr(&e.0, false)).collect::<Vec<_>>().join(", "), semicolon!()) },
|
||||
Expr::Tuple(elems) | Expr::Vector(elems) => { format!("[{}{}]", elems.iter().map(|e| self.gen_expr(&e.0, false)).collect::<Vec<_>>().join(", "), semicolon!()) },
|
||||
Expr::Object { fields } => {
|
||||
format!("{{{}}}",
|
||||
fields.iter().map(|(name, expr)| format!("{}: {}", name.0, self.gen_expr(&expr.0, false))).collect::<Vec<_>>().join(",\n "))
|
||||
|
|
|
@ -5,6 +5,7 @@ edition = "2021"
|
|||
|
||||
[dependencies]
|
||||
syntax = { path = "../syntax" }
|
||||
checker = { path = "../checker" }
|
||||
codegen = { path = "../codegen" }
|
||||
|
||||
colored = "2.0.0"
|
|
@ -31,6 +31,7 @@ fn main() {
|
|||
return;
|
||||
}
|
||||
|
||||
println!("{:#?}", ast.as_ref().unwrap());
|
||||
info!("Parsed in {}ms", time.elapsed().as_millis());
|
||||
|
||||
//
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
fun factorial (n : int) : int =
|
||||
factorial (n : int) : int =
|
||||
if n == 0
|
||||
| return 1
|
||||
| return n * factorial(n - 1)
|
||||
|
|
6
examples/generics.hz
Normal file
6
examples/generics.hz
Normal file
|
@ -0,0 +1,6 @@
|
|||
print T (x : T) : void = @write(x)
|
||||
|
||||
a : int = 123
|
||||
print(a)
|
||||
b : string = "Hello, World!"
|
||||
print(b)
|
|
@ -22,7 +22,7 @@ pub enum Token {
|
|||
Arrow, And, Or,
|
||||
|
||||
// Symbols & Delimiters
|
||||
Assign, Dot, Comma, Colon, Semicolon, At,
|
||||
Assign, Dot, Comma, Colon, Semicolon, At, Hash,
|
||||
Open(Delimiter), Close(Delimiter),
|
||||
}
|
||||
|
||||
|
@ -62,6 +62,7 @@ pub fn lexer() -> impl Parser<char, Vec<(Token, Span)>, Error = Simple<char>> {
|
|||
just(':').to(Token::Colon),
|
||||
just(';').to(Token::Semicolon),
|
||||
just('@').to(Token::At),
|
||||
just('#').to(Token::Hash),
|
||||
));
|
||||
|
||||
let delim = choice((
|
||||
|
|
|
@ -139,7 +139,7 @@ fn expr_parser() -> impl P<Spanned<Expr>> {
|
|||
.repeated())
|
||||
.foldl(|name, args| {( Expr::Intrinsic { name: Box::new(name.clone()), args }, name.1 )}).labelled("intrinsic");
|
||||
|
||||
let method = just(Token::Colon)
|
||||
let method = just(Token::Hash)
|
||||
.ignore_then(identexpr.clone())
|
||||
.then_ignore(just(Token::Dot))
|
||||
.then(atom.clone())
|
||||
|
@ -154,7 +154,7 @@ fn expr_parser() -> impl P<Spanned<Expr>> {
|
|||
args: args.into_iter().flatten().collect()
|
||||
}, span )}).labelled("method");
|
||||
|
||||
let access = just(Token::Semicolon)
|
||||
let access = just(Token::Colon)
|
||||
.ignore_then(identexpr)
|
||||
.then_ignore(just(Token::Dot))
|
||||
.then(atom.clone())
|
||||
|
@ -234,9 +234,8 @@ fn expr_parser() -> impl P<Spanned<Expr>> {
|
|||
(Expr::Redefine { name: *Box::new(ident), value: Box::new(expr) }, span)
|
||||
});
|
||||
|
||||
let function = just(Token::KwFun)
|
||||
.ignore_then(identifier())
|
||||
// Generics
|
||||
let function = identifier()
|
||||
// Generic
|
||||
.then(identifier().repeated())
|
||||
// Arguments
|
||||
.then(
|
||||
|
@ -300,7 +299,10 @@ fn expr_parser() -> impl P<Spanned<Expr>> {
|
|||
|
||||
#[allow(clippy::type_complexity)]
|
||||
pub fn parse(tokens: Vec<(Token, std::ops::Range<usize>)>, len: usize) -> (Option<Vec<(Expr, std::ops::Range<usize>)>>, Vec<Simple<Token>>) {
|
||||
let (ast, parse_error) = expr_parser().repeated().then_ignore(end()).parse_recovery(Stream::from_iter(
|
||||
let (ast, parse_error) = expr_parser()
|
||||
.repeated()
|
||||
.then_ignore(end())
|
||||
.parse_recovery(Stream::from_iter(
|
||||
len..len + 1,
|
||||
tokens.into_iter(),
|
||||
));
|
||||
|
|
Loading…
Reference in a new issue