Skip to content

Commit c38bd0a

Browse files
authored
Housekeeping (#7)
- Amend code a lil bit and add new lints - Fix clippy on CI
1 parent 80a835c commit c38bd0a

32 files changed

+677
-418
lines changed

.github/workflows/rust.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727
target/
2828
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
2929

30-
- uses: actions-rs/toolchain@v1
30+
- uses: actions-rs/toolchain@v1.0.6
3131
with:
3232
toolchain: nightly
3333

Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@ name = "luxya"
33
version = "0.1.0"
44
authors = ["franek <franeklubi@gmail.com>"]
55
edition = "2018"
6+
description = "Programming language with a tree-walking interpreter"
7+
license-file = "LICENSE"
8+
repository = "https://github.com/franeklubi/luxya/"
9+
keywords = [
10+
"programming-language",
11+
"interpreter",
12+
"programming-languages",
13+
"interpreted-programming-language",
14+
"tree-walk-interpreter",
15+
]
16+
categories = ["compilers"]
617

718
[dependencies]
819
exitcode = "1.1.2"

src/ast/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{ast::stmt::*, parser::types::Property, token::Token};
1+
use crate::{ast::stmt::Stmt, parser::types::Property, token::Token};
22
use std::{cell::Cell, rc::Rc};
33

44
#[derive(Clone)]
Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
use super::{helpers::*, types::*};
1+
use super::{
2+
helpers::assume_identifier,
3+
types::{InterpreterValue, RuntimeError},
4+
};
25
use crate::{
3-
env::*,
4-
token::*,
5-
// unwrap_enclosing,
6-
// unwrap_scope,
6+
env::{DeclaredValue, EnvironmentBase, EnvironmentWrapper},
7+
token::Token,
78
unwrap_scope_mut,
89
};
910

@@ -30,15 +31,13 @@ impl PartialEq for InterpreterEnvironment {
3031

3132
impl EnvironmentWrapper<InterpreterValue> for InterpreterEnvironment {
3233
fn new() -> Self {
33-
InterpreterEnvironment(Rc::new(RefCell::new(EnvironmentBase::new(
34-
None,
35-
))))
34+
Self(Rc::new(RefCell::new(EnvironmentBase::new(None))))
3635
}
3736

3837
fn fork(&self) -> Self {
39-
InterpreterEnvironment(Rc::new(RefCell::new(EnvironmentBase::new(
40-
Some(self.clone()),
41-
))))
38+
Self(Rc::new(RefCell::new(EnvironmentBase::new(Some(
39+
self.clone(),
40+
)))))
4241
}
4342

4443
fn read(

src/interpreter/expressions.rs

Lines changed: 60 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,52 @@
1-
use super::{helpers::*, interpret::*, interpreter_env::*, types::*};
2-
use crate::{ast::expr::*, env::*, token::*};
1+
use super::{
2+
env::InterpreterEnvironment,
3+
helpers::{
4+
assume_identifier,
5+
bind_function,
6+
confirm_arity,
7+
construct_lox_defined_function,
8+
extract_subscription_index,
9+
guard_function,
10+
map_arguments,
11+
unwrap_list,
12+
},
13+
interpret::{eval_expression, eval_statements},
14+
types::{InterpreterFunction, InterpreterValue, RuntimeError},
15+
};
16+
use crate::{
17+
ast::expr::{
18+
AssignmentValue,
19+
BinaryValue,
20+
CallValue,
21+
Expr,
22+
FunctionValue,
23+
GetAccessor,
24+
GetValue,
25+
IdentifierValue,
26+
LiteralValue,
27+
ObjectValue,
28+
SetValue,
29+
SuperAccessor,
30+
SuperValue,
31+
ThisValue,
32+
UnaryValue,
33+
},
34+
env::{DeclaredValue, EnvironmentWrapper},
35+
token::{Location, Token, TokenType},
36+
};
337

438
use std::{cell::RefCell, collections::HashMap, rc::Rc};
539

640

741
// inlining because it's used only once, but i wanted to take it
842
// out of the context, to make it less cluttery
9-
#[inline(always)]
43+
#[inline]
1044
pub fn literal_expression(
1145
v: &LiteralValue,
1246
env: &InterpreterEnvironment,
1347
) -> Result<InterpreterValue, RuntimeError> {
1448
match v {
15-
LiteralValue::String(s) => Ok(InterpreterValue::String(Rc::clone(&s))),
49+
LiteralValue::String(s) => Ok(InterpreterValue::String(Rc::clone(s))),
1650
LiteralValue::Number(n) => Ok(InterpreterValue::Number(*n)),
1751
LiteralValue::True => Ok(InterpreterValue::True),
1852
LiteralValue::False => Ok(InterpreterValue::False),
@@ -29,7 +63,7 @@ pub fn literal_expression(
2963
}
3064
}
3165

32-
#[inline(always)]
66+
#[inline]
3367
pub fn identifier_expression<E, T>(
3468
v: &IdentifierValue,
3569
env: &E,
@@ -40,7 +74,7 @@ where
4074
Ok(env.read(v.env_distance.get(), &v.name)?.value)
4175
}
4276

43-
#[inline(always)]
77+
#[inline]
4478
pub fn assignment_expression<E, T>(
4579
expr_evaluator: fn(&Expr, &E) -> Result<T, RuntimeError>,
4680
v: &AssignmentValue,
@@ -56,7 +90,7 @@ where
5690
)
5791
}
5892

59-
#[inline(always)]
93+
#[inline]
6094
pub fn call_expression(
6195
v: &CallValue,
6296
env: &InterpreterEnvironment,
@@ -90,7 +124,7 @@ pub fn execute_call(
90124
let fun_env = &enclosing_env.fork();
91125

92126
if let Some(params) = &fv.params {
93-
map_arguments(params, &arguments, fun_env)
127+
map_arguments(params, &arguments, fun_env);
94128
}
95129

96130
if let Some(statements) = &fv.body {
@@ -128,11 +162,11 @@ pub fn execute_call(
128162
}
129163
}
130164

131-
#[inline(always)]
165+
#[inline]
132166
pub fn function_expression(
133167
v: &FunctionValue,
134168
env: &InterpreterEnvironment,
135-
) -> Result<InterpreterValue, RuntimeError> {
169+
) -> InterpreterValue {
136170
let fun = construct_lox_defined_function(v, env);
137171

138172
if let Some(t) = &v.name {
@@ -147,7 +181,7 @@ pub fn function_expression(
147181
);
148182
}
149183

150-
Ok(fun)
184+
fun
151185
}
152186

153187
pub fn unary_expression(
@@ -228,7 +262,7 @@ pub fn binary_experssion(
228262
TokenType::LessEqual => Ok((n1 <= n2).into()),
229263
TokenType::Modulo => Ok(InterpreterValue::Number(n1 % n2)),
230264

231-
_ => unreachable!("Scanner did a bad job 😎."),
265+
_ => unreachable!("Scanner did a bad job \u{1f60e}."),
232266
}
233267
}
234268
(InterpreterValue::String(s1), InterpreterValue::String(s2)) => {
@@ -273,7 +307,7 @@ fn find_method(
273307
{
274308
(methods, superclass)
275309
} else {
276-
unreachable!("Class is not a class? 🤔")
310+
unreachable!("Class is not a class? \u{1f914}")
277311
};
278312

279313
if let Some(method) = methods.get(key) {
@@ -297,7 +331,7 @@ fn get_dot(
297331
) -> Result<InterpreterValue, RuntimeError> {
298332
// auxiliary function used only once down below, that's why inlining is
299333
// completely justified 🥺
300-
#[inline(always)]
334+
#[inline]
301335
fn get_property(
302336
key: &str,
303337
properties: &HashMap<String, InterpreterValue>,
@@ -336,18 +370,12 @@ fn get_dot(
336370

337371
match &v.key {
338372
GetAccessor::DotName(iden) => {
339-
get_property(iden, &borrowed_props, &class, &getee, &v.blame)
373+
get_property(iden, &borrowed_props, class, &getee, &v.blame)
340374
}
341375
GetAccessor::DotEval(expr) => {
342376
let key = eval_expression(expr, env)?.to_string();
343377

344-
get_property(
345-
&key.as_str(),
346-
&borrowed_props,
347-
&class,
348-
&getee,
349-
&v.blame,
350-
)
378+
get_property(key.as_str(), &borrowed_props, class, &getee, &v.blame)
351379
}
352380
_ => unreachable!("Wrong accessor in dot"),
353381
}
@@ -365,9 +393,9 @@ fn get_subscription(
365393
extract_subscription_index(&v.key, &v.blame, s.len(), env)?;
366394

367395
unsafe {
368-
Ok(InterpreterValue::Char(
369-
*s.as_bytes().get_unchecked(index) as char
370-
))
396+
Ok(InterpreterValue::Char(char::from(
397+
*s.as_bytes().get_unchecked(index),
398+
)))
371399
}
372400
}
373401
InterpreterValue::List(l) => {
@@ -389,7 +417,7 @@ fn get_subscription(
389417
}
390418
}
391419

392-
#[inline(always)]
420+
#[inline]
393421
pub fn get_expression(
394422
v: &GetValue,
395423
env: &InterpreterEnvironment,
@@ -463,7 +491,7 @@ fn set_subscription(
463491
Ok(value)
464492
}
465493

466-
#[inline(always)]
494+
#[inline]
467495
pub fn set_expression(
468496
v: &SetValue,
469497
env: &InterpreterEnvironment,
@@ -475,7 +503,7 @@ pub fn set_expression(
475503
}
476504
}
477505

478-
#[inline(always)]
506+
#[inline]
479507
pub fn this_expression<E, T>(v: &ThisValue, env: &E) -> Result<T, RuntimeError>
480508
where
481509
E: EnvironmentWrapper<T>,
@@ -518,7 +546,9 @@ pub fn super_expression(
518546
{
519547
constructor
520548
} else {
521-
unreachable!("Superclass should be a class like come on 🤦")
549+
unreachable!(
550+
"Superclass should be a class like come on \u{1f926}"
551+
)
522552
};
523553

524554
let constructor = constructor.ok_or_else(|| RuntimeError {
@@ -528,7 +558,7 @@ pub fn super_expression(
528558

529559
let constructor = bind_function(&constructor, instance);
530560

531-
execute_call(&constructor, &args, &v.blame, env)
561+
execute_call(&constructor, args, &v.blame, env)
532562
}
533563
}
534564
}

0 commit comments

Comments
 (0)