Skip to content

Commit

Permalink
added char expansion
Browse files Browse the repository at this point in the history
  • Loading branch information
sigmasoldi3r committed Nov 2, 2023
1 parent 7d2736a commit 36db999
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 52 deletions.
3 changes: 3 additions & 0 deletions examples/basic.saturn
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

// Declare variables
let some_var = "foo";
let some_char = 'a'; // This is a number actually.
some_char += 1;
print(some_var, some_char);

// Mutate them
some_var = 5;
Expand Down
2 changes: 1 addition & 1 deletion examples/extra_op.saturn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
let { operator, entries } = require('../src/assets/std');
let { operator, entries } = require("../src/assets/std");

@operator("|>")
fn pipe_right(left, right) {
Expand Down
2 changes: 1 addition & 1 deletion examples/loops.saturn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
let { entries } = require('../src/assets/std');
let { entries } = require("../src/assets/std");

let tbl = {
a: 1,
Expand Down
6 changes: 6 additions & 0 deletions src/code/generator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/// TODO
pub trait Generator {
fn gen() {
todo!("Not ready yet.")
}
}
2 changes: 2 additions & 0 deletions src/code/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
mod builder;
mod generator;
mod visitor;

pub use builder::Builder;
pub use generator::Generator;
pub use visitor::{VisitError, Visitor};

pub trait BuilderVisitor: Visitor<Builder> {}
36 changes: 0 additions & 36 deletions src/lua/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,38 +255,6 @@ impl code::Visitor<code::Builder> for LuaEmitter {
};
ctx.put(";")
}
ast::ClassField::Operator(f) => {
let target = match f.operator.0.as_str() {
"+" => "__add",
"-" => "__sub",
"*" => "__mul",
"/" => "__div",
"%" => "__mod",
"**" => "__pow",
"==" => "__eq",
"<" => "__lt",
"<=" => "__le",
"++" => "__concat",
"#?" => "__len",
_ => todo!(
"Operator overload for {:?} operator not supported",
f.operator.clone()
),
};
let ctx = ctx.put(format!(
"{}.prototype.__meta__.{} = ",
stmt.name.0.clone(),
target
));
let ctx = self.visit_lambda(
ctx,
&ast::Lambda {
arguments: f.arguments.clone(),
body: ast::ScriptOrExpression::Script(f.body.clone()),
},
)?;
ctx.put(";")
}
};
Ok(ctx)
})?;
Expand Down Expand Up @@ -334,8 +302,6 @@ impl code::Visitor<code::Builder> for LuaEmitter {
match native.iter().find(|(ident, _)| ident.0 == "Lua") {
Some((_, src)) => match src {
ast::StringLiteral::Double(src) => ctx.put(src),
ast::StringLiteral::Single(src) => ctx.put(src),
ast::StringLiteral::Special(_) => panic!("Not implemented"),
},
None => ctx.put("error('Native function implementation not found')"),
}
Expand Down Expand Up @@ -612,8 +578,6 @@ impl code::Visitor<code::Builder> for LuaEmitter {
) -> Result<code::Builder, code::VisitError> {
let ctx = match expr {
ast::StringLiteral::Double(s) => ctx.put("\"").put(escape_string(s.clone())).put("\""),
ast::StringLiteral::Single(s) => ctx.put("'").put(s.clone()).put("'"),
ast::StringLiteral::Special(_) => todo!(),
};
Ok(ctx)
}
Expand Down
12 changes: 1 addition & 11 deletions src/parser/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub struct Lambda {

#[derive(Debug, Clone)]
pub struct Do {
pub body: Script
pub body: Script,
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -186,18 +186,10 @@ pub enum Statement {
Expression(Expression),
}

#[derive(Debug, Clone)]
pub struct OperatorOverload {
pub operator: Operator,
pub arguments: Vec<Argument>,
pub body: Script,
}

#[derive(Debug, Clone)]
pub enum ClassField {
Method(Function),
Let(Let),
Operator(OperatorOverload),
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -246,8 +238,6 @@ pub enum TableKeyExpression {
#[derive(Debug, Clone)]
pub enum StringLiteral {
Double(String),
Single(String),
Special(String),
}

#[derive(Debug, Clone)]
Expand Down
4 changes: 1 addition & 3 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,11 @@ peg::parser! {
rule number_literal() -> Number
= value:$(DIGIT()+ "." DIGIT()+) { Number::Float(value.parse().unwrap()) }
/ value:$(DIGIT()+) { Number::Integer(value.parse().unwrap()) }
/ "'" value:$(!"'" ANY()) "'" { Number::Integer(value.chars().nth(0).unwrap() as i64) }
/ expected!("Number literal")

rule string_literal() -> StringLiteral
= "\"" value:$((!"\"" ANY())*) "\"" { StringLiteral::Double(value.into()) }
/ "'" value:$((!"'" ANY())*) "'" { StringLiteral::Single(value.into()) }
/ expected!("String literal")

rule vector_literal() -> Vector
Expand Down Expand Up @@ -269,8 +269,6 @@ peg::parser! {
rule class_fields() -> ClassField
= e:declare_var() { ClassField::Let(e) }
/ e:func() { ClassField::Method(e) }
/ "operator" _ operator:any_operator() _ arguments:argument_list() _ "{" body:script() "}"
{ ClassField::Operator(OperatorOverload { operator, arguments, body }) }

rule assign_extra() -> Operator
= value:$("++") { Operator(value.into()) }
Expand Down

0 comments on commit 36db999

Please sign in to comment.