Skip to content

Commit

Permalink
Housekeeping (#7)
Browse files Browse the repository at this point in the history
- Amend code a lil bit and add new lints

- Fix clippy on CI
  • Loading branch information
franeklubi authored Jun 6, 2021
1 parent 80a835c commit c38bd0a
Show file tree
Hide file tree
Showing 32 changed files with 677 additions and 418 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
target/
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}

- uses: actions-rs/toolchain@v1
- uses: actions-rs/toolchain@v1.0.6
with:
toolchain: nightly

Expand Down
11 changes: 11 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ name = "luxya"
version = "0.1.0"
authors = ["franek <franeklubi@gmail.com>"]
edition = "2018"
description = "Programming language with a tree-walking interpreter"
license-file = "LICENSE"
repository = "https://github.com/franeklubi/luxya/"
keywords = [
"programming-language",
"interpreter",
"programming-languages",
"interpreted-programming-language",
"tree-walk-interpreter",
]
categories = ["compilers"]

[dependencies]
exitcode = "1.1.2"
Expand Down
2 changes: 1 addition & 1 deletion src/ast/expr.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{ast::stmt::*, parser::types::Property, token::Token};
use crate::{ast::stmt::Stmt, parser::types::Property, token::Token};
use std::{cell::Cell, rc::Rc};

#[derive(Clone)]
Expand Down
21 changes: 10 additions & 11 deletions src/interpreter/interpreter_env.rs → src/interpreter/env.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use super::{helpers::*, types::*};
use super::{
helpers::assume_identifier,
types::{InterpreterValue, RuntimeError},
};
use crate::{
env::*,
token::*,
// unwrap_enclosing,
// unwrap_scope,
env::{DeclaredValue, EnvironmentBase, EnvironmentWrapper},
token::Token,
unwrap_scope_mut,
};

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

impl EnvironmentWrapper<InterpreterValue> for InterpreterEnvironment {
fn new() -> Self {
InterpreterEnvironment(Rc::new(RefCell::new(EnvironmentBase::new(
None,
))))
Self(Rc::new(RefCell::new(EnvironmentBase::new(None))))
}

fn fork(&self) -> Self {
InterpreterEnvironment(Rc::new(RefCell::new(EnvironmentBase::new(
Some(self.clone()),
))))
Self(Rc::new(RefCell::new(EnvironmentBase::new(Some(
self.clone(),
)))))
}

fn read(
Expand Down
90 changes: 60 additions & 30 deletions src/interpreter/expressions.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,52 @@
use super::{helpers::*, interpret::*, interpreter_env::*, types::*};
use crate::{ast::expr::*, env::*, token::*};
use super::{
env::InterpreterEnvironment,
helpers::{
assume_identifier,
bind_function,
confirm_arity,
construct_lox_defined_function,
extract_subscription_index,
guard_function,
map_arguments,
unwrap_list,
},
interpret::{eval_expression, eval_statements},
types::{InterpreterFunction, InterpreterValue, RuntimeError},
};
use crate::{
ast::expr::{
AssignmentValue,
BinaryValue,
CallValue,
Expr,
FunctionValue,
GetAccessor,
GetValue,
IdentifierValue,
LiteralValue,
ObjectValue,
SetValue,
SuperAccessor,
SuperValue,
ThisValue,
UnaryValue,
},
env::{DeclaredValue, EnvironmentWrapper},
token::{Location, Token, TokenType},
};

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


// inlining because it's used only once, but i wanted to take it
// out of the context, to make it less cluttery
#[inline(always)]
#[inline]
pub fn literal_expression(
v: &LiteralValue,
env: &InterpreterEnvironment,
) -> Result<InterpreterValue, RuntimeError> {
match v {
LiteralValue::String(s) => Ok(InterpreterValue::String(Rc::clone(&s))),
LiteralValue::String(s) => Ok(InterpreterValue::String(Rc::clone(s))),
LiteralValue::Number(n) => Ok(InterpreterValue::Number(*n)),
LiteralValue::True => Ok(InterpreterValue::True),
LiteralValue::False => Ok(InterpreterValue::False),
Expand All @@ -29,7 +63,7 @@ pub fn literal_expression(
}
}

#[inline(always)]
#[inline]
pub fn identifier_expression<E, T>(
v: &IdentifierValue,
env: &E,
Expand All @@ -40,7 +74,7 @@ where
Ok(env.read(v.env_distance.get(), &v.name)?.value)
}

#[inline(always)]
#[inline]
pub fn assignment_expression<E, T>(
expr_evaluator: fn(&Expr, &E) -> Result<T, RuntimeError>,
v: &AssignmentValue,
Expand All @@ -56,7 +90,7 @@ where
)
}

#[inline(always)]
#[inline]
pub fn call_expression(
v: &CallValue,
env: &InterpreterEnvironment,
Expand Down Expand Up @@ -90,7 +124,7 @@ pub fn execute_call(
let fun_env = &enclosing_env.fork();

if let Some(params) = &fv.params {
map_arguments(params, &arguments, fun_env)
map_arguments(params, &arguments, fun_env);
}

if let Some(statements) = &fv.body {
Expand Down Expand Up @@ -128,11 +162,11 @@ pub fn execute_call(
}
}

#[inline(always)]
#[inline]
pub fn function_expression(
v: &FunctionValue,
env: &InterpreterEnvironment,
) -> Result<InterpreterValue, RuntimeError> {
) -> InterpreterValue {
let fun = construct_lox_defined_function(v, env);

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

Ok(fun)
fun
}

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

_ => unreachable!("Scanner did a bad job 😎."),
_ => unreachable!("Scanner did a bad job \u{1f60e}."),
}
}
(InterpreterValue::String(s1), InterpreterValue::String(s2)) => {
Expand Down Expand Up @@ -273,7 +307,7 @@ fn find_method(
{
(methods, superclass)
} else {
unreachable!("Class is not a class? 🤔")
unreachable!("Class is not a class? \u{1f914}")
};

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

match &v.key {
GetAccessor::DotName(iden) => {
get_property(iden, &borrowed_props, &class, &getee, &v.blame)
get_property(iden, &borrowed_props, class, &getee, &v.blame)
}
GetAccessor::DotEval(expr) => {
let key = eval_expression(expr, env)?.to_string();

get_property(
&key.as_str(),
&borrowed_props,
&class,
&getee,
&v.blame,
)
get_property(key.as_str(), &borrowed_props, class, &getee, &v.blame)
}
_ => unreachable!("Wrong accessor in dot"),
}
Expand All @@ -365,9 +393,9 @@ fn get_subscription(
extract_subscription_index(&v.key, &v.blame, s.len(), env)?;

unsafe {
Ok(InterpreterValue::Char(
*s.as_bytes().get_unchecked(index) as char
))
Ok(InterpreterValue::Char(char::from(
*s.as_bytes().get_unchecked(index),
)))
}
}
InterpreterValue::List(l) => {
Expand All @@ -389,7 +417,7 @@ fn get_subscription(
}
}

#[inline(always)]
#[inline]
pub fn get_expression(
v: &GetValue,
env: &InterpreterEnvironment,
Expand Down Expand Up @@ -463,7 +491,7 @@ fn set_subscription(
Ok(value)
}

#[inline(always)]
#[inline]
pub fn set_expression(
v: &SetValue,
env: &InterpreterEnvironment,
Expand All @@ -475,7 +503,7 @@ pub fn set_expression(
}
}

#[inline(always)]
#[inline]
pub fn this_expression<E, T>(v: &ThisValue, env: &E) -> Result<T, RuntimeError>
where
E: EnvironmentWrapper<T>,
Expand Down Expand Up @@ -518,7 +546,9 @@ pub fn super_expression(
{
constructor
} else {
unreachable!("Superclass should be a class like come on 🤦")
unreachable!(
"Superclass should be a class like come on \u{1f926}"
)
};

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

let constructor = bind_function(&constructor, instance);

execute_call(&constructor, &args, &v.blame, env)
execute_call(&constructor, args, &v.blame, env)
}
}
}
Expand Down
Loading

0 comments on commit c38bd0a

Please # to comment.