1
1
Fork 0
mirror of https://github.com/azur1s/bobbylisp.git synced 2024-10-16 02:37:40 -05:00

Compare commits

..

No commits in common. "8c63304acf5cba6bf3e284daaa52c6ed6a64543c" and "2d45cf240b451f6239925b55a25e341aa15202e6" have entirely different histories.

5 changed files with 21 additions and 40 deletions

View file

@ -31,10 +31,9 @@ impl Codegen {
macro_rules! semicolon { () => { if should_gen_semicolon { ";" } else { "" } }; }
match ir {
IRKind::Define { name, type_hint, value, mutable } => {
IRKind::Define { name, type_hint, value } => {
format!(
"{} {}: {} = {}{}\n",
if *mutable { "let" } else { "const" },
"const {}: {} = {}{}\n",
name,
type_hint,
self.gen_ir(value, false),

View file

@ -13,7 +13,7 @@ pub enum Value { Int(i64), Boolean(bool), String(String), Ident(String) }
#[derive(Debug, Clone)]
pub enum IRKind {
Define { name: String, type_hint: String, value: Box<Self>, mutable: bool },
Define { name: String, type_hint: String, value: Box<Self> },
Fun { name: String, return_type_hint: String, args: Vec<(String, String)>, body: Box<Self> },
Call { name: String, args: Vec<Self> },
Intrinsic { name: String, args: Vec<Self> },
@ -115,16 +115,14 @@ pub fn expr_to_ir(expr: &Expr) -> (Option<IRKind>, Option<LoweringError>) {
_ => return (None, Some(LoweringError { span: name.1.clone(), message: "Expected identifier".to_string(), note: None }))
};
// Get all the `Hole` indexes
let mut indexes = Vec::new();
for (i, arg) in args.0.iter().enumerate() {
if let Expr::Hole(..) = &arg.0 {
indexes.push(i);
}
}
// Get the index where the `Hole` is at
let index = args.0.iter().position(|arg| match arg.0 {
Expr::Hole(..) => true,
_ => false
});
// If there is no `Hole` in the args then return early
if indexes.is_empty() {
if let None = index {
return (None, Some(LoweringError {
span: rhs.1.clone(),
message: "Expected hole in piping".to_string(),
@ -134,9 +132,7 @@ pub fn expr_to_ir(expr: &Expr) -> (Option<IRKind>, Option<LoweringError>) {
// Remove the `Hole` from the args
let mut new_args = args.0.clone();
for index in indexes.iter().rev() {
new_args.remove(*index);
}
new_args.remove(index.unwrap());
// Make a new call expression with the new args
let new_call = match call {
@ -186,18 +182,12 @@ pub fn expr_to_ir(expr: &Expr) -> (Option<IRKind>, Option<LoweringError>) {
};
},
Expr::Let { name, type_hint, value, mutable } => {
Expr::Let { name, type_hint, value } => {
let value = expr_to_ir(&value.0);
if_err_return!(value.1);
let value = value.0.unwrap();
let ir_kind = IRKind::Define {
name: name.clone(),
type_hint: gen_type_hint(type_hint),
value: Box::new(value),
mutable: *mutable
};
let ir_kind = IRKind::Define { name: name.clone(), type_hint: gen_type_hint(type_hint), value: Box::new(value) };
return (Some(ir_kind), None);
},

View file

@ -3,7 +3,7 @@ use chumsky::prelude::*;
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum Token {
// Keywords
KwLet, KwMut, KwFun,
KwLet, KwFun,
KwDo, KwEnd,
KwIf, KwThen, KwElse,
KwReturn,
@ -30,7 +30,6 @@ impl std::fmt::Display for Token {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Token::KwLet => write!(f, "let"),
Token::KwMut => write!(f, "mut"),
Token::KwFun => write!(f, "fun"),
Token::KwDo => write!(f, "do"),
Token::KwEnd => write!(f, "end"),

View file

@ -18,7 +18,6 @@ pub enum Expr {
name: String,
type_hint: String,
value: Box<Spanned<Self>>,
mutable: bool,
},
Fun {
name: String,
@ -194,19 +193,17 @@ fn expr_parser() -> impl Parser<Token, Vec<Spanned<Expr>>, Error = Simple<Token>
});
let let_ = just(Token::KwLet)
.ignore_then(just(Token::KwMut)).or_not()
.then(identifier)
.ignore_then(identifier)
.then_ignore(just(Token::Colon))
.then(identifier)
.then_ignore(just(Token::Assign))
.then(expr.clone())
.map(|(((mutable, name), type_hint), value)| {
.map(|((name, type_hint), value)| {
(
Expr::Let {
name: name.0.clone(),
type_hint: type_hint.0,
value: Box::new(value.clone()),
mutable: mutable.is_some(),
},
name.1.start..value.1.end,
)
@ -251,13 +248,13 @@ fn expr_parser() -> impl Parser<Token, Vec<Spanned<Expr>>, Error = Simple<Token>
)
});
let do_block = expr.clone()
let do_block = just(Token::KwDo)
.ignore_then(
expr.clone()
.then_ignore(just(Token::SemiColon))
.repeated()
.delimited_by(
just(Token::KwDo),
just(Token::KwEnd),
)
.then_ignore(just(Token::KwEnd))
.map_with_span(|body, span| {
(
Expr::Do {

View file

@ -12,8 +12,4 @@ fun main: void = do
|> qux(_)
|> quux(1, _)
|> @write(_);
210
|> bar(_, _)
|> @write(_);
end;