Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerlaberge committed Jun 30, 2018
2 parents 483a9cd + 72c04d3 commit 5247595
Show file tree
Hide file tree
Showing 10 changed files with 296 additions and 294 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rascal"
version = "1.1.0"
version = "1.2.0"
authors = ["tylerlaberge <[email protected]>"]

[dependencies]
Expand Down
190 changes: 95 additions & 95 deletions src/analyzer/semantic_analyzer.rs

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions src/analyzer/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ impl Symbol {

pub fn name(&self) -> String {
return match self {
&Symbol::Var(VarSymbol::INTEGER(ref name))
| &Symbol::Var(VarSymbol::REAL(ref name))
| &Symbol::Var(VarSymbol::STRING(ref name))
| &Symbol::Var(VarSymbol::BOOLEAN(ref name))
| &Symbol::Callable(CallableSymbol::Procedure(ref name, _))
| &Symbol::Callable(CallableSymbol::Function(ref name, _, _)) => name.clone()
Symbol::Var(VarSymbol::INTEGER(name))
| Symbol::Var(VarSymbol::REAL(name))
| Symbol::Var(VarSymbol::STRING(name))
| Symbol::Var(VarSymbol::BOOLEAN(name))
| Symbol::Callable(CallableSymbol::Procedure(name, _))
| Symbol::Callable(CallableSymbol::Function(name, _, _)) => name.clone()
};
}
}
108 changes: 54 additions & 54 deletions src/interpreter/interpreter.rs

Large diffs are not rendered by default.

88 changes: 44 additions & 44 deletions src/interpreter/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,21 @@ impl Object {

pub fn add(&self, other: &Self) -> Result<Self, String> {
return match (self, other) {
(&Object::Primitive(Primitive::Integer(ref left)), &Object::Primitive(Primitive::Integer(ref right)))
(Object::Primitive(Primitive::Integer(left)), Object::Primitive(Primitive::Integer(right)))
=> Ok(Object::Primitive(Primitive::Integer(left + right))),
(&Object::Primitive(Primitive::Float(ref left)), &Object::Primitive(Primitive::Float(ref right)))
(Object::Primitive(Primitive::Float(left)), Object::Primitive(Primitive::Float(right)))
=> Ok(Object::Primitive(Primitive::Float(left + right))),
(&Object::Primitive(Primitive::String(ref left)), &Object::Primitive(Primitive::String(ref right)))
(Object::Primitive(Primitive::String(left)), Object::Primitive(Primitive::String(right)))
=> Ok(Object::Primitive(Primitive::String(format!("{}{}", left, right)))),
_ => Err(String::from("Internal Interpreter Error: Attempted to add incompatible types"))
};
}

pub fn subtract(&self, other: &Self) -> Result<Self, String> {
return match (self, other) {
(&Object::Primitive(Primitive::Integer(ref left)), &Object::Primitive(Primitive::Integer(ref right)))
(Object::Primitive(Primitive::Integer(left)), Object::Primitive(Primitive::Integer(right)))
=> Ok(Object::Primitive(Primitive::Integer(left - right))),
(&Object::Primitive(Primitive::Float(ref left)), &Object::Primitive(Primitive::Float(ref right)))
(Object::Primitive(Primitive::Float(left)), Object::Primitive(Primitive::Float(right)))
=> Ok(Object::Primitive(Primitive::Float(left - right))),
_ => Err(String::from("Interpreter Interpreter Error: Attempted to subtract incompatible types"))

Expand All @@ -57,9 +57,9 @@ impl Object {

pub fn multiply(&self, other: &Self) -> Result<Self, String> {
return match (self, other) {
(&Object::Primitive(Primitive::Integer(ref left)), &Object::Primitive(Primitive::Integer(ref right)))
(Object::Primitive(Primitive::Integer(left)), Object::Primitive(Primitive::Integer(right)))
=> Ok(Object::Primitive(Primitive::Integer(left * right))),
(&Object::Primitive(Primitive::Float(ref left)), &Object::Primitive(Primitive::Float(ref right)))
(Object::Primitive(Primitive::Float(left)), Object::Primitive(Primitive::Float(right)))
=> Ok(Object::Primitive(Primitive::Float(left * right))),
_ => Err(String::from("Internal Interpreter Error: Attempted to multiply incompatible types"))

Expand All @@ -68,7 +68,7 @@ impl Object {

pub fn integer_divide(&self, other: &Self) -> Result<Self, String> {
return match (self, other) {
(&Object::Primitive(Primitive::Integer(ref left)), &Object::Primitive(Primitive::Integer(ref right)))
(Object::Primitive(Primitive::Integer(left)), Object::Primitive(Primitive::Integer(right)))
=> Ok(Object::Primitive(Primitive::Integer(left / right))),
_ => Err(String::from("Internal Interpreter Error: Attempted to integer divide incompatible types"))

Expand All @@ -77,7 +77,7 @@ impl Object {

pub fn float_divide(&self, other: &Self) -> Result<Self, String> {
return match (self, other) {
(&Object::Primitive(Primitive::Float(ref left)), &Object::Primitive(Primitive::Float(ref right)))
(Object::Primitive(Primitive::Float(left)), Object::Primitive(Primitive::Float(right)))
=> Ok(Object::Primitive(Primitive::Float(left / right))),
_ => Err(String::from("Internal Interpreter Error: Attempted to float divide incompatible types"))

Expand All @@ -86,99 +86,99 @@ impl Object {

pub fn unary_plus(&self) -> Result<Self, String> {
return match self {
&Object::Primitive(Primitive::Integer(i)) => Ok(Object::Primitive(Primitive::Integer(i))),
&Object::Primitive(Primitive::Float(i)) => Ok(Object::Primitive(Primitive::Float(i))),
_ => Err(String::from("Internal Interpreter Error: Attempted to unary add incompatible type"))
Object::Primitive(Primitive::Integer(i)) => Ok(Object::Primitive(Primitive::Integer(*i))),
Object::Primitive(Primitive::Float(i)) => Ok(Object::Primitive(Primitive::Float(*i))),
_ => Err(String::from("Internal Interpreter Error: Attempted to unary add incompatible type"))

};
}

pub fn unary_minus(&self) -> Result<Self, String> {
return match self {
&Object::Primitive(Primitive::Integer(i)) => Ok(Object::Primitive(Primitive::Integer(-i))),
&Object::Primitive(Primitive::Float(i)) => Ok(Object::Primitive(Primitive::Float(-i))),
_ => Err(String::from("Internal Interpreter Error: Attempted to unary subtract incompatible type"))
Object::Primitive(Primitive::Integer(i)) => Ok(Object::Primitive(Primitive::Integer(-i))),
Object::Primitive(Primitive::Float(i)) => Ok(Object::Primitive(Primitive::Float(-i))),
_ => Err(String::from("Internal Interpreter Error: Attempted to unary subtract incompatible type"))
};
}

pub fn and(&self, other: &Self) -> Result<Self, String> {
return match (self, other) {
(&Object::Primitive(Primitive::Boolean(left)), &Object::Primitive(Primitive::Boolean(right)))
=> Ok(Object::Primitive(Primitive::Boolean(left && right))),
(Object::Primitive(Primitive::Boolean(left)), Object::Primitive(Primitive::Boolean(right)))
=> Ok(Object::Primitive(Primitive::Boolean(*left && *right))),
_ => Err(String::from("Internal Interpreter Error: Attempted to boolean 'and' incompatible types"))
};
}

pub fn or(&self, other: &Self) -> Result<Self, String> {
return match (self, other) {
(&Object::Primitive(Primitive::Boolean(left)), &Object::Primitive(Primitive::Boolean(right)))
=> Ok(Object::Primitive(Primitive::Boolean(left || right))),
(Object::Primitive(Primitive::Boolean(left)), Object::Primitive(Primitive::Boolean(right)))
=> Ok(Object::Primitive(Primitive::Boolean(*left || *right))),
_ => Err(String::from("Internal Interpreter Error: Attempted to boolean 'or' incompatible types"))
};
}

pub fn negate(&self) -> Result<Self, String> {
return match self {
&Object::Primitive(Primitive::Boolean(b)) => Ok(Object::Primitive(Primitive::Boolean(!b))),
_ => Err(String::from("Internal Interpreter Error: Attempted to boolean negate incompatible type"))
Object::Primitive(Primitive::Boolean(b)) => Ok(Object::Primitive(Primitive::Boolean(!b))),
_ => Err(String::from("Internal Interpreter Error: Attempted to boolean negate incompatible type"))
};
}

pub fn less_than(&self, other: &Self) -> Result<Self, String> {
return match (self, other) {
(&Object::Primitive(Primitive::Integer(left)), &Object::Primitive(Primitive::Integer(right)))
(Object::Primitive(Primitive::Integer(left)), Object::Primitive(Primitive::Integer(right)))
=> Ok(Object::Primitive(Primitive::Boolean(left < right))),
(&Object::Primitive(Primitive::Float(left)), &Object::Primitive(Primitive::Float(right)))
(Object::Primitive(Primitive::Float(left)), Object::Primitive(Primitive::Float(right)))
=> Ok(Object::Primitive(Primitive::Boolean(left < right))),
_ => Err(String::from("Internal Interpreter Error: Attempted to compare 'less than' with incompatible types"))
};
}

pub fn less_than_or_equal(&self, other: &Self) -> Result<Self, String> {
return match (self, other) {
(&Object::Primitive(Primitive::Integer(left)), &Object::Primitive(Primitive::Integer(right)))
(Object::Primitive(Primitive::Integer(left)), Object::Primitive(Primitive::Integer(right)))
=> Ok(Object::Primitive(Primitive::Boolean(left <= right))),
(&Object::Primitive(Primitive::Float(left)), &Object::Primitive(Primitive::Float(right)))
(Object::Primitive(Primitive::Float(left)), Object::Primitive(Primitive::Float(right)))
=> Ok(Object::Primitive(Primitive::Boolean(left <= right))),
_ => Err(String::from("Internal Interpreter Error: Attempted to compare 'less than or equal' with incompatible types"))
};
}

pub fn greater_than(&self, other: &Self) -> Result<Self, String> {
return match (self, other) {
(&Object::Primitive(Primitive::Integer(left)), &Object::Primitive(Primitive::Integer(right)))
(Object::Primitive(Primitive::Integer(left)), Object::Primitive(Primitive::Integer(right)))
=> Ok(Object::Primitive(Primitive::Boolean(left > right))),
(&Object::Primitive(Primitive::Float(left)), &Object::Primitive(Primitive::Float(right)))
(Object::Primitive(Primitive::Float(left)), Object::Primitive(Primitive::Float(right)))
=> Ok(Object::Primitive(Primitive::Boolean(left > right))),
_ => Err(String::from("Internal Interpreter Error: Attempted to compare 'greater than' with incompatible types"))
};
}

pub fn greater_than_or_equal(&self, other: &Self) -> Result<Self, String> {
return match (self, other) {
(&Object::Primitive(Primitive::Integer(left)), &Object::Primitive(Primitive::Integer(right)))
(Object::Primitive(Primitive::Integer(left)), Object::Primitive(Primitive::Integer(right)))
=> Ok(Object::Primitive(Primitive::Boolean(left >= right))),
(&Object::Primitive(Primitive::Float(left)), &Object::Primitive(Primitive::Float(right)))
(Object::Primitive(Primitive::Float(left)), Object::Primitive(Primitive::Float(right)))
=> Ok(Object::Primitive(Primitive::Boolean(left >= right))),
_ => Err(String::from("Internal Interpreter Error: Attempted to compare 'greater than or equal' with incompatible types"))
};
}

pub fn equal(&self, other: &Self) -> Result<Self, String> {
return match (self, other) {
(&Object::Primitive(Primitive::Integer(left)), &Object::Primitive(Primitive::Integer(right)))
(Object::Primitive(Primitive::Integer(left)), Object::Primitive(Primitive::Integer(right)))
=> Ok(Object::Primitive(Primitive::Boolean(left == right))),
(&Object::Primitive(Primitive::Float(left)), &Object::Primitive(Primitive::Float(right)))
(Object::Primitive(Primitive::Float(left)), Object::Primitive(Primitive::Float(right)))
=> Ok(Object::Primitive(Primitive::Boolean(left == right))),
_ => Err(String::from("Internal Interpreter Error: Attempted to compare 'equal' with incompatible types"))
};
}

pub fn not_equal(&self, other: &Self) -> Result<Self, String> {
return match (self, other) {
(&Object::Primitive(Primitive::Integer(left)), &Object::Primitive(Primitive::Integer(right)))
(Object::Primitive(Primitive::Integer(left)), Object::Primitive(Primitive::Integer(right)))
=> Ok(Object::Primitive(Primitive::Boolean(left != right))),
(&Object::Primitive(Primitive::Float(left)), &Object::Primitive(Primitive::Float(right)))
(Object::Primitive(Primitive::Float(left)), Object::Primitive(Primitive::Float(right)))
=> Ok(Object::Primitive(Primitive::Boolean(left != right))),
_ => Err(String::from("Internal Interpreter Error: Attempted to compare 'not equal' with incompatible types"))
};
Expand All @@ -189,17 +189,17 @@ impl Object {
impl Debug for Object {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
return match self {
&Object::Unit => write!(f, "<Unit>"),
&Object::Primitive(ref primitive) => write!(f, "{:?}", primitive),
&Object::Procedure(ref name, ref variables, _) => write!(f, "Procedure<{}, {:?}>", name, variables),
&Object::Function(ref name, ref variables, _, ref return_type) => write!(f, "Function<{}, {:?} -> {:?}>", name, variables, return_type),
&Object::BuiltInFunction(BuiltInFunction::Write(_)) => write!(f, "BuiltInFunction<Write, (String) -> ()>"),
&Object::BuiltInFunction(BuiltInFunction::WriteLn(_)) => write!(f, "BuiltInFunction<WriteLn, (String) -> ()>"),
&Object::BuiltInFunction(BuiltInFunction::ReadLn(_)) => write!(f, "BuiltInFunction<ReadLn, () -> String>"),
&Object::BuiltInFunction(BuiltInFunction::IntToString(_)) => write!(f, "BuiltInFunction<IntToString, (Integer) -> String>"),
&Object::BuiltInFunction(BuiltInFunction::RealToString(_)) => write!(f, "BuiltInFunction<RealToString, (Real) -> String>"),
&Object::BuiltInFunction(BuiltInFunction::StringToInt(_)) => write!(f, "BuiltInFunction<StringToInt, (String) -> Integer>"),
&Object::BuiltInFunction(BuiltInFunction::StringToReal(_)) => write!(f, "BuiltInFunction<StringToReal, (String) -> Real>")
Object::Unit => write!(f, "<Unit>"),
Object::Primitive(primitive) => write!(f, "{:?}", primitive),
Object::Procedure(name, variables, _) => write!(f, "Procedure<{}, {:?}>", name, variables),
Object::Function(name, variables, _, return_type) => write!(f, "Function<{}, {:?} -> {:?}>", name, variables, return_type),
Object::BuiltInFunction(BuiltInFunction::Write(_)) => write!(f, "BuiltInFunction<Write, (String) -> ()>"),
Object::BuiltInFunction(BuiltInFunction::WriteLn(_)) => write!(f, "BuiltInFunction<WriteLn, (String) -> ()>"),
Object::BuiltInFunction(BuiltInFunction::ReadLn(_)) => write!(f, "BuiltInFunction<ReadLn, () -> String>"),
Object::BuiltInFunction(BuiltInFunction::IntToString(_)) => write!(f, "BuiltInFunction<IntToString, (Integer) -> String>"),
Object::BuiltInFunction(BuiltInFunction::RealToString(_)) => write!(f, "BuiltInFunction<RealToString, (Real) -> String>"),
Object::BuiltInFunction(BuiltInFunction::StringToInt(_)) => write!(f, "BuiltInFunction<StringToInt, (String) -> Integer>"),
Object::BuiltInFunction(BuiltInFunction::StringToReal(_)) => write!(f, "BuiltInFunction<StringToReal, (String) -> Real>")
};
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/lexer/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,18 +153,18 @@ impl<'a> Lexer<'a> {
fn comparison(&mut self) -> Result<Token, String> {
return match self.source.current_char() {
Some('<') => match self.source.peek() {
Some(&'=') => {
Some('=') => {
self.source.next();
Ok(Token::LESS_THAN_OR_EQUAL)
},
Some(&'>') => {
Some('>') => {
self.source.next();
Ok(Token::NOT_EQUAL)
},
_ => Ok(Token::LESS_THAN)
},
Some('>') => match self.source.peek() {
Some(&'=') => {
Some('=') => {
self.source.next();
Ok(Token::GREATER_THAN_OR_EQUAL)
},
Expand All @@ -181,7 +181,7 @@ impl<'a> Lexer<'a> {
Some(character) if character.is_digit(10) => self.integer(),
Some(character) if character.is_alphabetic() => self.id(),
Some(':') => match self.source.peek() {
Some(&'=') => self.assign(),
Some('=') => self.assign(),
_ => Ok(Token::COLON)
},
Some('\'') => self.string(),
Expand Down
8 changes: 5 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ use std::env;
use std::io::Read;
use std::fs::File;

fn main() {
let filename = get_file_name().unwrap();
let source = read_source_file(filename.as_str()).unwrap();
fn main() -> Result<(), String> {
let filename = get_file_name()?;
let source = read_source_file(filename.as_str())?;

rascal::interpret(source);

Ok(())
}

fn get_file_name() -> Result<String, String> {
Expand Down
Loading

0 comments on commit 5247595

Please sign in to comment.