Skip to content

Commit 973e306

Browse files
committed
Use Rc instead of Box
It's **much** faster with the current bytecode implementation (~33%) and will likely remain so in the future.
1 parent a0c5b6c commit 973e306

File tree

7 files changed

+241
-161
lines changed

7 files changed

+241
-161
lines changed

script/examples/vec2.bs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
fn main()
2-
print(length(0.5, 0.7))
2+
for _ in 10_000_000
3+
#empty()
4+
args(0.5, 0.7)
5+
#length(0.5, 0.7)
6+
#print(length(0.5, 0.7))
7+
pass
38

49

510
fn length(x, y)
611
return (x * x + y * y).sqrt()
12+
13+
fn empty()
14+
pass
15+
16+
fn args(x, y)
17+
pass

script/src/ast.rs

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub(crate) enum Expression<'src> {
6060
right: Box<Expression<'src>>,
6161
},
6262
Function {
63-
expr: Option<Box<Expression<'src>>>,
63+
expr: Option<Box<Expression<'src>>>,
6464
name: &'src str,
6565
arguments: Vec<Expression<'src>>,
6666
},
@@ -184,17 +184,23 @@ impl<'src> Function<'src> {
184184
match tokens.next() {
185185
Some((Token::BracketRoundOpen, l, c)) => match tokens.next() {
186186
Some((Token::BracketRoundClose, ..)) => (),
187-
Some((pre, ..)) => {
187+
Some((mut pre, ..)) => {
188188
loop {
189189
let (expr, last_tk) = Expression::parse(pre, tokens)?;
190190
args.push(expr);
191191
match last_tk {
192192
Some(Token::Comma) => (),
193193
Some(Token::BracketRoundClose) => break,
194+
None => err!(UnexpectedEOF, 0, 0),
194195
tk => {
195196
panic!("Expression did not parse all tokens: {:?}", tk)
196197
}
197198
}
199+
pre = if let Some((tk, ..)) = tokens.next() {
200+
tk
201+
} else {
202+
err!(UnexpectedEOF, 0, 0);
203+
};
198204
}
199205
lines.push(Statement::Call { func: name, args });
200206
}
@@ -245,6 +251,7 @@ impl<'src> Function<'src> {
245251
}
246252
}
247253
Some((Token::Indent(0), ..)) | None => return Ok((lines, 0)),
254+
Some((Token::Indent(i), ..)) if i < expected_indent => return Ok((lines, 0)),
248255
Some((Token::Indent(i), ..)) if i == expected_indent => (),
249256
Some((Token::Indent(i), l, c)) => err!(UnexpectedIndent, i, l, c),
250257
Some((tk, ..)) => todo!("{:?}", tk),
@@ -293,8 +300,15 @@ impl<'src> Expression<'src> {
293300
None => err!(UnexpectedEOF, 0, 0),
294301
}
295302
}
296-
let expr = None;
297-
(Expression::Function { expr, name, arguments }, None)
303+
let expr = None;
304+
(
305+
Expression::Function {
306+
expr,
307+
name,
308+
arguments,
309+
},
310+
None,
311+
)
298312
}
299313
Some((tk, ..)) => (Expression::Atom(Atom::Name(name)), Some(tk)),
300314
e => todo!("{:?}", e),
@@ -305,7 +319,7 @@ impl<'src> Expression<'src> {
305319
match last_tk {
306320
Token::Op(opl) => match tokens.next() {
307321
Some((Token::Name(mid), ..)) => {
308-
let og_mid = mid;
322+
let og_mid = mid;
309323
let mid = Expression::Atom(Atom::Name(mid));
310324
match tokens.next() {
311325
Some((Token::Op(opr), ..)) => match tokens.next() {
@@ -335,20 +349,21 @@ impl<'src> Expression<'src> {
335349
}
336350
e => todo!("{:?}", e),
337351
},
338-
Some((Token::BracketRoundOpen, ..)) => {
339-
// FIXME handle function args & check for ending brace
340-
tokens.next();
341-
match opl {
342-
Op::Access => {
343-
Ok((Expression::Function {
344-
expr: Some(Box::new(lhs)),
345-
name: og_mid,
346-
arguments: Vec::new(),
347-
}, Some(Token::BracketRoundClose)))
348-
}
349-
e => todo!("{:?}", e),
350-
}
351-
}
352+
Some((Token::BracketRoundOpen, ..)) => {
353+
// FIXME handle function args & check for ending brace
354+
tokens.next();
355+
match opl {
356+
Op::Access => Ok((
357+
Expression::Function {
358+
expr: Some(Box::new(lhs)),
359+
name: og_mid,
360+
arguments: Vec::new(),
361+
},
362+
Some(Token::BracketRoundClose),
363+
)),
364+
e => todo!("{:?}", e),
365+
}
366+
}
352367
Some((tk, ..)) if tk == Token::BracketRoundClose => Ok((
353368
Expression::Operation {
354369
left: Box::new(lhs),
@@ -379,8 +394,7 @@ impl<'src> Expression<'src> {
379394
}
380395
Some((Token::Comma, ..)) => return Ok((lhs, Some(Token::Comma))),
381396
Some((Token::Indent(i), ..)) => return Ok((lhs, Some(Token::Indent(i)))),
382-
Some((tk, ..)) => todo!("{:?}", tk),
383-
None => todo!("none"),
397+
e => todo!("{:?}", e),
384398
}
385399
}
386400
}

script/src/bin.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ pub fn main() -> Result<(), io::Error> {
1111
Ok(source) => match ballscript::parse(&source) {
1212
Ok(script) => {
1313
let mut script = script.instance();
14-
dbg!(&script);
1514
match script.call("main", &[]) {
1615
Ok(_) => Ok(()),
1716
Err(e) => todo!("{:?}", e),

0 commit comments

Comments
 (0)