mirror of
https://github.com/azur1s/bobbylisp.git
synced 2024-10-16 02:37:40 -05:00
one or more holes in piping, (almost) mutable variable
This commit is contained in:
parent
2d45cf240b
commit
419d152cf7
|
@ -31,9 +31,10 @@ impl Codegen {
|
||||||
macro_rules! semicolon { () => { if should_gen_semicolon { ";" } else { "" } }; }
|
macro_rules! semicolon { () => { if should_gen_semicolon { ";" } else { "" } }; }
|
||||||
|
|
||||||
match ir {
|
match ir {
|
||||||
IRKind::Define { name, type_hint, value } => {
|
IRKind::Define { name, type_hint, value, mutable } => {
|
||||||
format!(
|
format!(
|
||||||
"const {}: {} = {}{}\n",
|
"{} {}: {} = {}{}\n",
|
||||||
|
if *mutable { "let" } else { "const" },
|
||||||
name,
|
name,
|
||||||
type_hint,
|
type_hint,
|
||||||
self.gen_ir(value, false),
|
self.gen_ir(value, false),
|
||||||
|
|
|
@ -13,7 +13,7 @@ pub enum Value { Int(i64), Boolean(bool), String(String), Ident(String) }
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum IRKind {
|
pub enum IRKind {
|
||||||
Define { name: String, type_hint: String, value: Box<Self> },
|
Define { name: String, type_hint: String, value: Box<Self>, mutable: bool },
|
||||||
Fun { name: String, return_type_hint: String, args: Vec<(String, String)>, body: Box<Self> },
|
Fun { name: String, return_type_hint: String, args: Vec<(String, String)>, body: Box<Self> },
|
||||||
Call { name: String, args: Vec<Self> },
|
Call { name: String, args: Vec<Self> },
|
||||||
Intrinsic { name: String, args: Vec<Self> },
|
Intrinsic { name: String, args: Vec<Self> },
|
||||||
|
@ -115,14 +115,16 @@ 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 }))
|
_ => return (None, Some(LoweringError { span: name.1.clone(), message: "Expected identifier".to_string(), note: None }))
|
||||||
};
|
};
|
||||||
|
|
||||||
// Get the index where the `Hole` is at
|
// Get all the `Hole` indexes
|
||||||
let index = args.0.iter().position(|arg| match arg.0 {
|
let mut indexes = Vec::new();
|
||||||
Expr::Hole(..) => true,
|
for (i, arg) in args.0.iter().enumerate() {
|
||||||
_ => false
|
if let Expr::Hole(..) = &arg.0 {
|
||||||
});
|
indexes.push(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// If there is no `Hole` in the args then return early
|
// If there is no `Hole` in the args then return early
|
||||||
if let None = index {
|
if indexes.is_empty() {
|
||||||
return (None, Some(LoweringError {
|
return (None, Some(LoweringError {
|
||||||
span: rhs.1.clone(),
|
span: rhs.1.clone(),
|
||||||
message: "Expected hole in piping".to_string(),
|
message: "Expected hole in piping".to_string(),
|
||||||
|
@ -132,7 +134,9 @@ pub fn expr_to_ir(expr: &Expr) -> (Option<IRKind>, Option<LoweringError>) {
|
||||||
|
|
||||||
// Remove the `Hole` from the args
|
// Remove the `Hole` from the args
|
||||||
let mut new_args = args.0.clone();
|
let mut new_args = args.0.clone();
|
||||||
new_args.remove(index.unwrap());
|
for index in indexes.iter().rev() {
|
||||||
|
new_args.remove(*index);
|
||||||
|
}
|
||||||
|
|
||||||
// Make a new call expression with the new args
|
// Make a new call expression with the new args
|
||||||
let new_call = match call {
|
let new_call = match call {
|
||||||
|
@ -182,12 +186,18 @@ pub fn expr_to_ir(expr: &Expr) -> (Option<IRKind>, Option<LoweringError>) {
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
Expr::Let { name, type_hint, value } => {
|
Expr::Let { name, type_hint, value, mutable } => {
|
||||||
let value = expr_to_ir(&value.0);
|
let value = expr_to_ir(&value.0);
|
||||||
if_err_return!(value.1);
|
if_err_return!(value.1);
|
||||||
|
|
||||||
let value = value.0.unwrap();
|
let value = value.0.unwrap();
|
||||||
let ir_kind = IRKind::Define { name: name.clone(), type_hint: gen_type_hint(type_hint), value: Box::new(value) };
|
let ir_kind = IRKind::Define {
|
||||||
|
name: name.clone(),
|
||||||
|
type_hint: gen_type_hint(type_hint),
|
||||||
|
value: Box::new(value),
|
||||||
|
mutable: *mutable
|
||||||
|
};
|
||||||
|
|
||||||
return (Some(ir_kind), None);
|
return (Some(ir_kind), None);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@ use chumsky::prelude::*;
|
||||||
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||||||
pub enum Token {
|
pub enum Token {
|
||||||
// Keywords
|
// Keywords
|
||||||
KwLet, KwFun,
|
KwLet, KwMut, KwFun,
|
||||||
KwDo, KwEnd,
|
KwDo, KwEnd,
|
||||||
KwIf, KwThen, KwElse,
|
KwIf, KwThen, KwElse,
|
||||||
KwReturn,
|
KwReturn,
|
||||||
|
@ -30,6 +30,7 @@ impl std::fmt::Display for Token {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
Token::KwLet => write!(f, "let"),
|
Token::KwLet => write!(f, "let"),
|
||||||
|
Token::KwMut => write!(f, "mut"),
|
||||||
Token::KwFun => write!(f, "fun"),
|
Token::KwFun => write!(f, "fun"),
|
||||||
Token::KwDo => write!(f, "do"),
|
Token::KwDo => write!(f, "do"),
|
||||||
Token::KwEnd => write!(f, "end"),
|
Token::KwEnd => write!(f, "end"),
|
||||||
|
|
|
@ -18,6 +18,7 @@ pub enum Expr {
|
||||||
name: String,
|
name: String,
|
||||||
type_hint: String,
|
type_hint: String,
|
||||||
value: Box<Spanned<Self>>,
|
value: Box<Spanned<Self>>,
|
||||||
|
mutable: bool,
|
||||||
},
|
},
|
||||||
Fun {
|
Fun {
|
||||||
name: String,
|
name: String,
|
||||||
|
@ -193,17 +194,19 @@ fn expr_parser() -> impl Parser<Token, Vec<Spanned<Expr>>, Error = Simple<Token>
|
||||||
});
|
});
|
||||||
|
|
||||||
let let_ = just(Token::KwLet)
|
let let_ = just(Token::KwLet)
|
||||||
.ignore_then(identifier)
|
.ignore_then(just(Token::KwMut)).or_not()
|
||||||
|
.then(identifier)
|
||||||
.then_ignore(just(Token::Colon))
|
.then_ignore(just(Token::Colon))
|
||||||
.then(identifier)
|
.then(identifier)
|
||||||
.then_ignore(just(Token::Assign))
|
.then_ignore(just(Token::Assign))
|
||||||
.then(expr.clone())
|
.then(expr.clone())
|
||||||
.map(|((name, type_hint), value)| {
|
.map(|(((mutable, name), type_hint), value)| {
|
||||||
(
|
(
|
||||||
Expr::Let {
|
Expr::Let {
|
||||||
name: name.0.clone(),
|
name: name.0.clone(),
|
||||||
type_hint: type_hint.0,
|
type_hint: type_hint.0,
|
||||||
value: Box::new(value.clone()),
|
value: Box::new(value.clone()),
|
||||||
|
mutable: mutable.is_some(),
|
||||||
},
|
},
|
||||||
name.1.start..value.1.end,
|
name.1.start..value.1.end,
|
||||||
)
|
)
|
||||||
|
|
|
@ -12,4 +12,8 @@ fun main: void = do
|
||||||
|> qux(_)
|
|> qux(_)
|
||||||
|> quux(1, _)
|
|> quux(1, _)
|
||||||
|> @write(_);
|
|> @write(_);
|
||||||
|
|
||||||
|
210
|
||||||
|
|> bar(_, _)
|
||||||
|
|> @write(_);
|
||||||
end;
|
end;
|
Loading…
Reference in a new issue