Skip to content

Commit

Permalink
Some reordering for clarity and minor performance (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
JanEricNitschke authored Apr 28, 2024
1 parent 99ac94c commit 218bc36
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 46 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ more-benchmark: $(REL_BIN)
for filename in benchmark/*.gen; do \
echo $$filename; \
filebase=$${filename%.*}; \
hyperfine --show-output --warmup 1 ".\\target\\release\\generic $${filebase}.gen" ".\\reference\\craftinginterpreters\\clox $${filebase}.lox.nom" ".\\reference\\craftinginterpreters\\jlox.bat $${filebase}.lox.nom"; \
hyperfine --reference ".\\target\\release\\generic $${filebase}.gen" --show-output --warmup 1 ".\\reference\\craftinginterpreters\\clox $${filebase}.lox.nom" ".\\reference\\craftinginterpreters\\jlox.bat $${filebase}.lox.nom"; \
done

.PHONY: fib-benchmark
fib-benchmark: $(REL_BIN)
hyperfine --warmup 1 ".\\target\release\\generic benchmark\\fib\\fib.gen" ".\\reference\\craftinginterpreters\\clox benchmark\\fib\\fib.lox.nom" \
hyperfine --reference ".\\target\release\\generic benchmark\\fib\\fib.gen" --warmup 1 ".\\reference\\craftinginterpreters\\clox benchmark\\fib\\fib.lox.nom" \
"python benchmark\\fib\\fib.py" "ruby benchmark\\fib\\fib.rb" ".\\reference\\craftinginterpreters\\jlox.bat benchmark\\fib\\fib.lox.nom" \

.PHONY: benchmark-ci
Expand Down
11 changes: 6 additions & 5 deletions src/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ impl TryFrom<ConstantLongIndex> for ConstantIndex {
pub enum OpCode {
Constant,
ConstantLong,
Closure,

DefineGlobal,
DefineGlobalLong,
Expand Down Expand Up @@ -72,10 +71,11 @@ pub enum OpCode {
StopIteration,

Equal,
Greater,
Less,
NotEqual,

Greater,
GreaterEqual,
Less,
LessEqual,

LoadOne,
Expand Down Expand Up @@ -106,13 +106,14 @@ pub enum OpCode {
DupN,
Swap,

Invoke,
Method,
Closure,
Return,

Class,
SetProperty,
GetProperty,
Method,
Invoke,
Inherit,
GetSuper,
SuperInvoke,
Expand Down
24 changes: 12 additions & 12 deletions src/compiler/front.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,14 +260,14 @@ impl<'scanner, 'heap> Compiler<'scanner, 'heap> {
}

pub(super) fn declaration(&mut self) {
if self.match_(TK::Class) {
self.class_declaration();
} else if self.match_(TK::Fun) {
self.fun_declaration();
} else if self.match_(TK::Var) {
if self.match_(TK::Var) {
self.var_declaration(true);
} else if self.match_(TK::Const) {
self.var_declaration(false);
} else if self.match_(TK::Fun) {
self.fun_declaration();
} else if self.match_(TK::Class) {
self.class_declaration();
} else {
self.statement();
}
Expand All @@ -277,24 +277,24 @@ impl<'scanner, 'heap> Compiler<'scanner, 'heap> {
}

fn statement(&mut self) {
if self.match_(TK::For) {
if self.match_(TK::If) || self.match_(TK::Unless) {
self.conditional_statement(self.check_previous(TK::If));
} else if self.match_(TK::LeftBrace) {
self.scoped_block();
} else if self.match_(TK::For) {
self.for_statement();
} else if self.match_(TK::ForEach) {
self.foreach_statement();
} else if self.match_(TK::If) || self.match_(TK::Unless) {
self.conditional_statement(self.check_previous(TK::If));
} else if self.match_(TK::Return) {
self.return_statement();
} else if self.match_(TK::While) || self.match_(TK::Until) {
self.loop_statement(self.check_previous(TK::While));
} else if self.match_(TK::Switch) {
self.switch_statement();
} else if self.match_(TK::Continue) {
self.continue_statement();
} else if self.match_(TK::Break) {
self.break_statement();
} else if self.match_(TK::LeftBrace) {
self.scoped_block();
} else if self.match_(TK::Switch) {
self.switch_statement();
} else if self.match_(TK::Import) {
self.import_statement();
} else if self.match_(TK::From) {
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ pub(super) fn make_rules<'scanner, 'arena>() -> Rules<'scanner, 'arena> {
Identifier = [variable, None, None ],
In = [None, binary, In ],
String = [string, None, None ],
Number = [number, None, None ],
Float = [number, None, None ],
Integer = [integer, None, None ],
And = [None, and, And ],
Case = [None, None, None ],
Expand Down
53 changes: 27 additions & 26 deletions src/scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ pub enum TokenKind {
RightBrace,
LeftBracket,
RightBracket,
Colon,

Colon,
Comma,
Dot,

Minus,
MinusEqual,
Plus,
Expand All @@ -37,11 +38,8 @@ pub enum TokenKind {
AmperEqual,
Hat,
HatEqual,

StarStar,
SlashSlash,

// One Or Two Character Tokens.
Bang,
BangEqual,
Equal,
Expand All @@ -51,38 +49,43 @@ pub enum TokenKind {
Less,
LessEqual,

In,

// Literals.
Identifier,
String,
Number,
Float,
Integer,
False,
True,
Nil,
StopIteration,

// Keywords.
And,
Case,
Class,
Continue,
Break,
Default,
Or,
If,
Unless,
Else,
False,
For,
ForEach,
Fun,
If,
In,
Unless,
Nil,
Or,
Return,
While,
Until,
Continue,
Break,
Switch,
Super,
This,
True,
Default,
Case,

Const,
Var,
While,
Until,

Class,
This,
Super,

Fun,
Return,

Error,
Eof,
Expand All @@ -94,8 +97,6 @@ pub enum TokenKind {
Yield,
Async,
Await,

StopIteration,
}

impl std::fmt::Display for TokenKind {
Expand Down Expand Up @@ -320,7 +321,7 @@ impl<'a> Scanner<'a> {
return self.make_token(TokenKind::Integer);
}

self.make_token(TokenKind::Number)
self.make_token(TokenKind::Float)
}

#[allow(clippy::trivially_copy_pass_by_ref)]
Expand Down

0 comments on commit 218bc36

Please sign in to comment.