aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/interpret/operator.rs148
-rw-r--r--src/interpret/scope.rs18
-rw-r--r--src/interpret/value.rs46
-rw-r--r--src/interpret/walker.rs26
-rw-r--r--src/lex/lexer.rs62
-rw-r--r--src/lex/token.rs10
-rw-r--r--src/parse/ast/nodes.rs16
-rw-r--r--src/parse/macros.rs22
-rw-r--r--src/parse/parser.rs14
-rw-r--r--src/types/bag.rs40
-rw-r--r--src/types/mod.rs6
11 files changed, 202 insertions, 206 deletions
diff --git a/src/interpret/operator.rs b/src/interpret/operator.rs
index 78ebe75..ce062c7 100644
--- a/src/interpret/operator.rs
+++ b/src/interpret/operator.rs
@@ -3,7 +3,7 @@ use thiserror::Error;
use crate::{parse::ast::expression::Expression, types::bag::TypeBag};
use super::{
- value::{Value, ValueVariant},
+ value::{Value, ValueKind},
walker::{Walker, WalkerError},
};
@@ -17,23 +17,23 @@ impl<'t> ValueOperator<'t> {
}
pub fn add(&self, lhs: Value, rhs: Value) -> Result<Value, OperationError> {
- match lhs.variant {
- ValueVariant::Str(ref l) => match rhs.variant {
- ValueVariant::Str(r) => Ok(Value::str(format!("{}{}", l, r), self.types)),
- ValueVariant::Float(r) => Ok(Value::str(format!("{}{}", l, r), self.types)),
- ValueVariant::Int(r) => Ok(Value::str(format!("{}{}", l, r), self.types)),
+ match lhs.kind {
+ ValueKind::Str(ref l) => match rhs.kind {
+ ValueKind::Str(r) => Ok(Value::str(format!("{}{}", l, r), self.types)),
+ ValueKind::Float(r) => Ok(Value::str(format!("{}{}", l, r), self.types)),
+ ValueKind::Int(r) => Ok(Value::str(format!("{}{}", l, r), self.types)),
_ => Err(OperationError::AddTypes(lhs, rhs)),
},
- ValueVariant::Float(l) => match rhs.variant {
- ValueVariant::Str(r) => Ok(Value::str(l.to_string() + &r, self.types)),
- ValueVariant::Float(r) => Ok(Value::float(l + r, self.types)),
- ValueVariant::Int(r) => Ok(Value::float(l + r as f64, self.types)),
+ ValueKind::Float(l) => match rhs.kind {
+ ValueKind::Str(r) => Ok(Value::str(l.to_string() + &r, self.types)),
+ ValueKind::Float(r) => Ok(Value::float(l + r, self.types)),
+ ValueKind::Int(r) => Ok(Value::float(l + r as f64, self.types)),
_ => Err(OperationError::AddTypes(lhs, rhs)),
},
- ValueVariant::Int(l) => match rhs.variant {
- ValueVariant::Str(r) => Ok(Value::str(l.to_string() + &r, self.types)),
- ValueVariant::Float(r) => Ok(Value::float(l as f64 + r, self.types)),
- ValueVariant::Int(r) => Ok(Value::int(l + r, self.types)),
+ ValueKind::Int(l) => match rhs.kind {
+ ValueKind::Str(r) => Ok(Value::str(l.to_string() + &r, self.types)),
+ ValueKind::Float(r) => Ok(Value::float(l as f64 + r, self.types)),
+ ValueKind::Int(r) => Ok(Value::int(l + r, self.types)),
_ => Err(OperationError::AddTypes(lhs, rhs)),
},
_ => Err(OperationError::AddTypes(lhs, rhs)),
@@ -41,15 +41,15 @@ impl<'t> ValueOperator<'t> {
}
pub fn sub(&self, lhs: Value, rhs: Value) -> Result<Value, OperationError> {
- match lhs.variant {
- ValueVariant::Float(l) => match rhs.variant {
- ValueVariant::Float(r) => Ok(Value::float(l - r, self.types)),
- ValueVariant::Int(r) => Ok(Value::float(l - r as f64, self.types)),
+ match lhs.kind {
+ ValueKind::Float(l) => match rhs.kind {
+ ValueKind::Float(r) => Ok(Value::float(l - r, self.types)),
+ ValueKind::Int(r) => Ok(Value::float(l - r as f64, self.types)),
_ => Err(OperationError::SubTypes(lhs, rhs)),
},
- ValueVariant::Int(l) => match rhs.variant {
- ValueVariant::Float(r) => Ok(Value::float(l as f64 - r, self.types)),
- ValueVariant::Int(r) => Ok(Value::int(l - r, self.types)),
+ ValueKind::Int(l) => match rhs.kind {
+ ValueKind::Float(r) => Ok(Value::float(l as f64 - r, self.types)),
+ ValueKind::Int(r) => Ok(Value::int(l - r, self.types)),
_ => Err(OperationError::SubTypes(lhs, rhs)),
},
_ => Err(OperationError::SubTypes(lhs, rhs)),
@@ -57,15 +57,15 @@ impl<'t> ValueOperator<'t> {
}
pub fn mul(&self, lhs: Value, rhs: Value) -> Result<Value, OperationError> {
- match lhs.variant {
- ValueVariant::Float(l) => match rhs.variant {
- ValueVariant::Float(r) => Ok(Value::float(l * r, self.types)),
- ValueVariant::Int(r) => Ok(Value::float(l * r as f64, self.types)),
+ match lhs.kind {
+ ValueKind::Float(l) => match rhs.kind {
+ ValueKind::Float(r) => Ok(Value::float(l * r, self.types)),
+ ValueKind::Int(r) => Ok(Value::float(l * r as f64, self.types)),
_ => Err(OperationError::MulTypes(lhs, rhs)),
},
- ValueVariant::Int(l) => match rhs.variant {
- ValueVariant::Float(r) => Ok(Value::float(l as f64 * r, self.types)),
- ValueVariant::Int(r) => Ok(Value::int(l * r, self.types)),
+ ValueKind::Int(l) => match rhs.kind {
+ ValueKind::Float(r) => Ok(Value::float(l as f64 * r, self.types)),
+ ValueKind::Int(r) => Ok(Value::int(l * r, self.types)),
_ => Err(OperationError::MulTypes(lhs, rhs)),
},
_ => Err(OperationError::MulTypes(lhs, rhs)),
@@ -73,15 +73,15 @@ impl<'t> ValueOperator<'t> {
}
pub fn div(&self, lhs: Value, rhs: Value) -> Result<Value, OperationError> {
- match lhs.variant {
- ValueVariant::Float(l) => match rhs.variant {
- ValueVariant::Float(r) => Ok(Value::float(l / r, self.types)),
- ValueVariant::Int(r) => Ok(Value::float(l / r as f64, self.types)),
+ match lhs.kind {
+ ValueKind::Float(l) => match rhs.kind {
+ ValueKind::Float(r) => Ok(Value::float(l / r, self.types)),
+ ValueKind::Int(r) => Ok(Value::float(l / r as f64, self.types)),
_ => Err(OperationError::DivTypes(lhs, rhs)),
},
- ValueVariant::Int(l) => match rhs.variant {
- ValueVariant::Float(r) => Ok(Value::float(l as f64 / r, self.types)),
- ValueVariant::Int(r) => Ok(Value::float(l as f64 / r as f64, self.types)),
+ ValueKind::Int(l) => match rhs.kind {
+ ValueKind::Float(r) => Ok(Value::float(l as f64 / r, self.types)),
+ ValueKind::Int(r) => Ok(Value::float(l as f64 / r as f64, self.types)),
_ => Err(OperationError::DivTypes(lhs, rhs)),
},
_ => Err(OperationError::DivTypes(lhs, rhs)),
@@ -89,21 +89,21 @@ impl<'t> ValueOperator<'t> {
}
pub fn eq(&self, lhs: Value, rhs: Value) -> Result<Value, OperationError> {
- match lhs.variant {
- ValueVariant::Str(l) => match rhs.variant {
- ValueVariant::Str(r) => Ok(Value::bool(l == r, self.types)),
+ match lhs.kind {
+ ValueKind::Str(l) => match rhs.kind {
+ ValueKind::Str(r) => Ok(Value::bool(l == r, self.types)),
_ => Ok(Value::bool(false, self.types)),
},
- ValueVariant::Float(l) => match rhs.variant {
- ValueVariant::Float(r) => Ok(Value::bool(l == r, self.types)),
+ ValueKind::Float(l) => match rhs.kind {
+ ValueKind::Float(r) => Ok(Value::bool(l == r, self.types)),
_ => Ok(Value::bool(false, self.types)),
},
- ValueVariant::Int(l) => match rhs.variant {
- ValueVariant::Int(r) => Ok(Value::bool(l == r, self.types)),
+ ValueKind::Int(l) => match rhs.kind {
+ ValueKind::Int(r) => Ok(Value::bool(l == r, self.types)),
_ => Ok(Value::bool(false, self.types)),
},
- ValueVariant::Bool(l) => match rhs.variant {
- ValueVariant::Bool(r) => Ok(Value::bool(l == r, self.types)),
+ ValueKind::Bool(l) => match rhs.kind {
+ ValueKind::Bool(r) => Ok(Value::bool(l == r, self.types)),
_ => Ok(Value::bool(false, self.types)),
},
_ => Ok(Value::bool(false, self.types)),
@@ -112,7 +112,7 @@ impl<'t> ValueOperator<'t> {
pub fn neq(&self, lhs: Value, rhs: Value) -> Result<Value, OperationError> {
if let Ok(Value {
- variant: ValueVariant::Bool(value),
+ kind: ValueKind::Bool(value),
..
}) = self.eq(lhs, rhs)
{
@@ -123,15 +123,15 @@ impl<'t> ValueOperator<'t> {
}
pub fn gt(&self, lhs: Value, rhs: Value) -> Result<Value, OperationError> {
- match lhs.variant {
- ValueVariant::Float(r) => match rhs.variant {
- ValueVariant::Float(l) => Ok(Value::bool(r > l, self.types)),
- ValueVariant::Int(l) => Ok(Value::bool(r > l as f64, self.types)),
+ match lhs.kind {
+ ValueKind::Float(r) => match rhs.kind {
+ ValueKind::Float(l) => Ok(Value::bool(r > l, self.types)),
+ ValueKind::Int(l) => Ok(Value::bool(r > l as f64, self.types)),
_ => Err(OperationError::CompareTypes(lhs, rhs)),
},
- ValueVariant::Int(r) => match rhs.variant {
- ValueVariant::Float(l) => Ok(Value::bool(r as f64 > l, self.types)),
- ValueVariant::Int(l) => Ok(Value::bool(r > l, self.types)),
+ ValueKind::Int(r) => match rhs.kind {
+ ValueKind::Float(l) => Ok(Value::bool(r as f64 > l, self.types)),
+ ValueKind::Int(l) => Ok(Value::bool(r > l, self.types)),
_ => Err(OperationError::CompareTypes(lhs, rhs)),
},
_ => Err(OperationError::CompareTypes(lhs, rhs)),
@@ -139,15 +139,15 @@ impl<'t> ValueOperator<'t> {
}
pub fn gte(&self, lhs: Value, rhs: Value) -> Result<Value, OperationError> {
- match lhs.variant {
- ValueVariant::Float(r) => match rhs.variant {
- ValueVariant::Float(l) => Ok(Value::bool(r >= l, self.types)),
- ValueVariant::Int(l) => Ok(Value::bool(r >= l as f64, self.types)),
+ match lhs.kind {
+ ValueKind::Float(r) => match rhs.kind {
+ ValueKind::Float(l) => Ok(Value::bool(r >= l, self.types)),
+ ValueKind::Int(l) => Ok(Value::bool(r >= l as f64, self.types)),
_ => Err(OperationError::CompareTypes(lhs, rhs)),
},
- ValueVariant::Int(r) => match rhs.variant {
- ValueVariant::Float(l) => Ok(Value::bool(r as f64 >= l, self.types)),
- ValueVariant::Int(l) => Ok(Value::bool(r >= l, self.types)),
+ ValueKind::Int(r) => match rhs.kind {
+ ValueKind::Float(l) => Ok(Value::bool(r as f64 >= l, self.types)),
+ ValueKind::Int(l) => Ok(Value::bool(r >= l, self.types)),
_ => Err(OperationError::CompareTypes(lhs, rhs)),
},
_ => Err(OperationError::CompareTypes(lhs, rhs)),
@@ -155,28 +155,28 @@ impl<'t> ValueOperator<'t> {
}
pub fn neg(&self, val: Value) -> Result<Value, OperationError> {
- match val.variant {
- ValueVariant::Float(float) => Ok(Value::float(-float, self.types)),
- ValueVariant::Int(int) => Ok(Value::int(-int, self.types)),
+ match val.kind {
+ ValueKind::Float(float) => Ok(Value::float(-float, self.types)),
+ ValueKind::Int(int) => Ok(Value::int(-int, self.types)),
_ => Err(OperationError::NegType(val)),
}
}
pub fn not(&self, val: Value) -> Result<Value, OperationError> {
- match val.variant {
- ValueVariant::Bool(bool) => Ok(Value::bool(bool, self.types)),
+ match val.kind {
+ ValueKind::Bool(bool) => Ok(Value::bool(bool, self.types)),
_ => Err(OperationError::NotType(val)),
}
}
pub fn subscript(&self, val: Value, index: Value) -> Result<Value, OperationError> {
- let index = match index.variant {
- ValueVariant::Int(i) => i,
+ let index = match index.kind {
+ ValueKind::Int(i) => i,
_ => return Err(OperationError::ArrayIndexType(index)),
};
- match val.variant {
- ValueVariant::Array(a) => {
+ match val.kind {
+ ValueKind::Array(a) => {
let array = a.borrow();
if index < 0 || index as usize >= array.len() {
Err(OperationError::ArrayIndexOutOfRange {
@@ -198,13 +198,13 @@ impl<'t> ValueOperator<'t> {
index: Value,
value: Value,
) -> Result<Value, OperationError> {
- let index = match index.variant {
- ValueVariant::Int(i) => i,
+ let index = match index.kind {
+ ValueKind::Int(i) => i,
_ => return Err(OperationError::ArrayIndexType(index)),
};
- match &val.variant {
- ValueVariant::Array(a) => {
+ match &val.kind {
+ ValueKind::Array(a) => {
let mut array = a.borrow_mut();
if index < 0 || index as usize >= array.len() {
Err(OperationError::ArrayIndexOutOfRange {
@@ -221,8 +221,8 @@ impl<'t> ValueOperator<'t> {
}
pub fn call(&self, val: &Value, arguments: Vec<Value>) -> Result<Value, WalkerError> {
- let called = match &val.variant {
- ValueVariant::Fn(i) => i,
+ let called = match &val.kind {
+ ValueKind::Fn(i) => i,
_ => {
return Err(WalkerError::OperationError(OperationError::CallableType(
val.clone(),
diff --git a/src/interpret/scope.rs b/src/interpret/scope.rs
index 3813cab..8a5db83 100644
--- a/src/interpret/scope.rs
+++ b/src/interpret/scope.rs
@@ -1,7 +1,7 @@
-use super::value::{Value, ValueVariant};
+use super::value::{Value, ValueKind};
use crate::{
parse::ast::nodes::Identifier,
- types::{bag::TypeBag, TypeVariant},
+ types::{bag::TypeBag, TypeKind},
};
use std::{cell::RefCell, collections::HashMap, rc::Rc};
use thiserror::Error;
@@ -117,23 +117,23 @@ impl AssignedValue {
fn get_value(&self, types: &TypeBag) -> Value {
match self {
Self::Mutable(value) => value.clone(),
- Self::Constant(value) => match &value.variant {
- ValueVariant::Array(reference) => {
+ Self::Constant(value) => match &value.kind {
+ ValueKind::Array(reference) => {
let underlying_value = reference.borrow().clone();
Value {
- variant: ValueVariant::Array(Rc::new(RefCell::new(underlying_value))),
+ kind: ValueKind::Array(Rc::new(RefCell::new(underlying_value))),
// FIXME: Give arrays actual type instead of void.
- typ: types.create_type(TypeVariant::Array(types.void())),
+ typ: types.create_type(TypeKind::Array(types.void())),
}
}
- ValueVariant::Fn(reference) => {
+ ValueKind::Fn(reference) => {
let underlying_value = reference.borrow().clone();
Value {
- variant: ValueVariant::Fn(Rc::new(RefCell::new(underlying_value))),
+ kind: ValueKind::Fn(Rc::new(RefCell::new(underlying_value))),
// FIXME: Give functions actual types.
- typ: types.create_type(TypeVariant::Fn {
+ typ: types.create_type(TypeKind::Fn {
parameters: HashMap::new(),
returns: types.void(),
}),
diff --git a/src/interpret/value.rs b/src/interpret/value.rs
index 45c177d..6d46a0d 100644
--- a/src/interpret/value.rs
+++ b/src/interpret/value.rs
@@ -10,12 +10,12 @@ type ReferenceOnCopy<T> = Rc<RefCell<T>>;
#[derive(Clone, Debug)]
pub struct Value {
- pub variant: ValueVariant,
+ pub kind: ValueKind,
pub typ: Type,
}
#[derive(Clone, Debug)]
-pub enum ValueVariant {
+pub enum ValueKind {
Str(String),
Float(f64),
Int(i64),
@@ -29,49 +29,49 @@ pub enum ValueVariant {
impl Value {
pub fn str(val: String, types: &TypeBag) -> Self {
Self {
- variant: ValueVariant::Str(val),
+ kind: ValueKind::Str(val),
typ: types.str(),
}
}
pub fn int(val: i64, types: &TypeBag) -> Self {
Self {
- variant: ValueVariant::Int(val),
+ kind: ValueKind::Int(val),
typ: types.int(),
}
}
pub fn float(val: f64, types: &TypeBag) -> Self {
Self {
- variant: ValueVariant::Float(val),
+ kind: ValueKind::Float(val),
typ: types.float(),
}
}
pub fn bool(val: bool, types: &TypeBag) -> Self {
Self {
- variant: ValueVariant::Bool(val),
+ kind: ValueKind::Bool(val),
typ: types.bool(),
}
}
pub fn void(types: &TypeBag) -> Self {
Self {
- variant: ValueVariant::Void,
+ kind: ValueKind::Void,
typ: types.void(),
}
}
pub fn type_name(&self) -> &'static str {
// TODO: Base this off of the type.
- match self.variant {
- ValueVariant::Str(_) => "Str",
- ValueVariant::Float(_) => "Float",
- ValueVariant::Int(_) => "Int",
- ValueVariant::Bool(_) => "Bool",
- ValueVariant::Array(_) => "Array",
- ValueVariant::Fn(_) => "Fn",
- ValueVariant::Void => "Void",
+ match self.kind {
+ ValueKind::Str(_) => "Str",
+ ValueKind::Float(_) => "Float",
+ ValueKind::Int(_) => "Int",
+ ValueKind::Bool(_) => "Bool",
+ ValueKind::Array(_) => "Array",
+ ValueKind::Fn(_) => "Fn",
+ ValueKind::Void => "Void",
_ => todo!(),
}
}
@@ -79,12 +79,12 @@ impl Value {
impl Display for Value {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- match &self.variant {
- ValueVariant::Str(v) => write!(f, "{}", v),
- ValueVariant::Float(v) => write!(f, "{}", v),
- ValueVariant::Int(v) => write!(f, "{}", v),
- ValueVariant::Bool(v) => write!(f, "{}", v),
- ValueVariant::Array(a) => {
+ match &self.kind {
+ ValueKind::Str(v) => write!(f, "{}", v),
+ ValueKind::Float(v) => write!(f, "{}", v),
+ ValueKind::Int(v) => write!(f, "{}", v),
+ ValueKind::Bool(v) => write!(f, "{}", v),
+ ValueKind::Array(a) => {
write!(
f,
"[{}]",
@@ -95,8 +95,8 @@ impl Display for Value {
.join(", ")
)
}
- ValueVariant::Fn(v) => write!(f, "<fn {:?}>", v.as_ptr()),
- ValueVariant::Void => write!(f, "<void>"),
+ ValueKind::Fn(v) => write!(f, "<fn {:?}>", v.as_ptr()),
+ ValueKind::Void => write!(f, "<void>"),
_ => todo!(),
}
}
diff --git a/src/interpret/walker.rs b/src/interpret/walker.rs
index 171f4d1..01bde11 100644
--- a/src/interpret/walker.rs
+++ b/src/interpret/walker.rs
@@ -3,7 +3,7 @@ use std::{cell::RefCell, collections::HashMap, rc::Rc};
use crate::{
interpret::{
operator::ValueOperator,
- value::{FnValue, ValueVariant},
+ value::{FnValue, ValueKind},
},
parse::ast::{
expression::Expression,
@@ -13,7 +13,7 @@ use crate::{
statement::Statement,
Program,
},
- types::{bag::TypeBag, TypeVariant},
+ types::{bag::TypeBag, TypeKind},
};
use thiserror::Error;
@@ -93,8 +93,8 @@ impl Walker {
// Short-circuting operators
if let BinOp::And | BinOp::Or = op {
let left_value = self.walk_expression(left)?;
- let is_left_true = match left_value.variant {
- ValueVariant::Bool(bool) => bool,
+ let is_left_true = match left_value.kind {
+ ValueKind::Bool(bool) => bool,
_ => return Err(WalkerError::WrongAndOrType),
};
@@ -170,11 +170,9 @@ impl Walker {
elements.push(self.walk_expression(expression)?);
}
Ok(Value {
- variant: ValueVariant::Array(Rc::new(RefCell::new(elements))),
+ kind: ValueKind::Array(Rc::new(RefCell::new(elements))),
// FIXME: Use actual type.
- typ: self
- .types
- .create_type(TypeVariant::Array(self.types.void())),
+ typ: self.types.create_type(TypeKind::Array(self.types.void())),
})
}
Expression::SimpleLiteral(token) => {
@@ -202,20 +200,20 @@ impl Walker {
Ok(Value::str(buffer, &self.types))
}
Expression::FnLiteral(node) => Ok(Value {
- variant: ValueVariant::Fn(Rc::new(RefCell::new(FnValue {
+ kind: ValueKind::Fn(Rc::new(RefCell::new(FnValue {
node: node.as_ref().clone(),
scope: self.scope.clone(),
}))),
// FIXME: Use actual type here.
- typ: self.types.create_type(TypeVariant::Fn {
+ typ: self.types.create_type(TypeKind::Fn {
parameters: HashMap::new(),
returns: self.types.void(),
}),
}),
Expression::If(if_node) => {
for conditional in &if_node.conditionals {
- if let ValueVariant::Bool(bool) =
- self.walk_expression(&conditional.condition)?.variant
+ if let ValueKind::Bool(bool) =
+ self.walk_expression(&conditional.condition)?.kind
{
if bool {
return self.walk_block(&conditional.block);
@@ -234,8 +232,8 @@ impl Walker {
Expression::Loop(loop_node) => {
if let Some(condition) = &loop_node.condition {
loop {
- if let ValueVariant::Bool(should_repeat) =
- self.walk_expression(condition)?.variant
+ if let ValueKind::Bool(should_repeat) =
+ self.walk_expression(condition)?.kind
{
if should_repeat {
match self.walk_block(&loop_node.body) {
diff --git a/src/lex/lexer.rs b/src/lex/lexer.rs
index a4818fe..276702a 100644
--- a/src/lex/lexer.rs
+++ b/src/lex/lexer.rs
@@ -1,6 +1,6 @@
use std::{collections::VecDeque, iter::Peekable, str::Chars};
-use super::token::{Location, Token, TokenVariant};
+use super::token::{Location, Token, TokenKind};
pub struct Lexer<'source> {
location: Location,
@@ -14,7 +14,7 @@ impl Iterator for Lexer<'_> {
type Item = Token;
fn next(&mut self) -> Option<Self::Item> {
- use super::token::TokenVariant::*;
+ use super::token::TokenKind::*;
if self.done {
return None;
@@ -31,7 +31,7 @@ impl Iterator for Lexer<'_> {
self.done = true;
return Some(Token {
location: self.location,
- variant: Eof,
+ kind: Eof,
});
}
@@ -47,7 +47,7 @@ impl Iterator for Lexer<'_> {
let location = self.location;
// Fixed length tokens
- let variant = match self.advance().unwrap() {
+ let kind = match self.advance().unwrap() {
'+' => OpPlus,
'-' => {
if self.advance_if('>') {
@@ -119,7 +119,7 @@ impl Iterator for Lexer<'_> {
_ => Unknown(c),
};
- Token { location, variant }
+ Token { location, kind }
};
Some(token)
@@ -188,15 +188,15 @@ impl<'s> Lexer<'s> {
buffer.push(c);
}
- let variant = if is_integer {
+ let kind = if is_integer {
let int = buffer.parse().expect("Failed lexing integer token.");
- TokenVariant::Int(int)
+ TokenKind::Int(int)
} else {
let float = buffer.parse().expect("Failed lexing float token.");
- TokenVariant::Float(float)
+ TokenKind::Float(float)
};
- Token { location, variant }
+ Token { location, kind }
}
fn identifier(&mut self) -> Token {
@@ -209,25 +209,25 @@ impl<'s> Lexer<'s> {
buffer.push(c);
}
- let variant = match buffer.as_str() {
- "fn" => TokenVariant::KeywordFn,
- "if" => TokenVariant::KeywordIf,
- "elif" => TokenVariant::KeywordElif,
- "else" => TokenVariant::KeywordElse,
- "loop" => TokenVariant::KeywordLoop,
- "type" => TokenVariant::KeywordType,
- "form" => TokenVariant::KeywordForm,
- "self" => TokenVariant::KeywordSelf,
- "true" => TokenVariant::KeywordTrue,
- "false" => TokenVariant::KeywordFalse,
- "return" => TokenVariant::KeywordReturn,
- "break" => TokenVariant::KeywordBreak,
- "continue" => TokenVariant::KeywordContinue,
- "print" => TokenVariant::KeywordPrint,
- _ => TokenVariant::Ident(buffer),
+ let kind = match buffer.as_str() {
+ "fn" => TokenKind::KeywordFn,
+ "if" => TokenKind::KeywordIf,
+ "elif" => TokenKind::KeywordElif,
+ "else" => TokenKind::KeywordElse,
+ "loop" => TokenKind::KeywordLoop,
+ "type" => TokenKind::KeywordType,
+ "form" => TokenKind::KeywordForm,
+ "self" => TokenKind::KeywordSelf,
+ "true" => TokenKind::KeywordTrue,
+ "false" => TokenKind::KeywordFalse,
+ "return" => TokenKind::KeywordReturn,
+ "break" => TokenKind::KeywordBreak,
+ "continue" => TokenKind::KeywordContinue,
+ "print" => TokenKind::KeywordPrint,
+ _ => TokenKind::Ident(buffer),
};
- Token { location, variant }
+ Token { location, kind }
}
fn str(&mut self) -> Token {
@@ -247,7 +247,7 @@ impl<'s> Lexer<'s> {
if !str_buffer.is_empty() {
self.preempted.push_back(Token {
location: location_str,
- variant: TokenVariant::Str(str_buffer),
+ kind: TokenKind::Str(str_buffer),
});
}
str_buffer = String::new();
@@ -265,7 +265,7 @@ impl<'s> Lexer<'s> {
if !str_buffer.is_empty() {
self.preempted.push_back(Token {
location: location_str,
- variant: TokenVariant::Str(str_buffer),
+ kind: TokenKind::Str(str_buffer),
});
}
@@ -276,12 +276,12 @@ impl<'s> Lexer<'s> {
col: self.location.col - 1,
row: self.location.row,
},
- variant: TokenVariant::StrClose,
+ kind: TokenKind::StrClose,
});
Token {
location: location_start,
- variant: TokenVariant::StrOpen,
+ kind: TokenKind::StrOpen,
}
}
@@ -314,7 +314,7 @@ impl<'s> Lexer<'s> {
// Finish embed
self.preempted.push_back(Token {
location: location_embed,
- variant: TokenVariant::StrEmbed(embed_buffer),
+ kind: TokenKind::StrEmbed(embed_buffer),
});
}
diff --git a/src/lex/token.rs b/src/lex/token.rs
index c771b4a..f3dafa7 100644
--- a/src/lex/token.rs
+++ b/src/lex/token.rs
@@ -7,11 +7,11 @@ pub struct Location {
#[derive(Clone, Debug)]
pub struct Token {
pub location: Location,
- pub variant: TokenVariant,
+ pub kind: TokenKind,
}
#[derive(Clone, Debug, PartialEq)]
-pub enum TokenVariant {
+pub enum TokenKind {
// Basic math operators
OpPlus,
OpMinus,
@@ -49,7 +49,7 @@ pub enum TokenVariant {
// Literals
Int(u32),
Float(f32),
-
+
// String Literal Tokens
Str(String),
// StrOpen and StrClose are necessary for string embeds.
@@ -59,7 +59,7 @@ pub enum TokenVariant {
StrClose,
// The string embed has to be lexed by a *seperate* lexer.
StrEmbed(String),
-
+
Ident(String),
// Keywords
@@ -80,4 +80,4 @@ pub enum TokenVariant {
Unknown(char),
Eof,
-} \ No newline at end of file
+}
diff --git a/src/parse/ast/nodes.rs b/src/parse/ast/nodes.rs
index c4bb1e8..16d0eaa 100644
--- a/src/parse/ast/nodes.rs
+++ b/src/parse/ast/nodes.rs
@@ -1,4 +1,4 @@
-use crate::lex::token::{Token, TokenVariant::*};
+use crate::lex::token::{Token, TokenKind::*};
use super::{expression::Expression, statement::Statement};
@@ -23,7 +23,7 @@ pub enum BinaryOperator {
impl BinaryOperator {
pub fn from_token(token: Token) -> Self {
- match token.variant {
+ match token.kind {
OpPlus => Self::Plus,
OpMinus => Self::Minus,
OpStar => Self::Star,
@@ -39,7 +39,7 @@ impl BinaryOperator {
Assign => Self::Assign,
ConstAssign => Self::ConstAssign,
Dot => Self::Dot,
- _ => panic!("Can't create binary operator from '{:?}'.", token.variant),
+ _ => panic!("Can't create binary operator from '{:?}'.", token.kind),
}
}
}
@@ -52,10 +52,10 @@ pub enum UnaryOperator {
impl UnaryOperator {
pub fn from_token(token: Token) -> Self {
- match token.variant {
+ match token.kind {
OpMinus => Self::Minus,
OpNot => Self::Not,
- _ => panic!("Can't create unary operator from '{:?}'.", token.variant),
+ _ => panic!("Can't create unary operator from '{:?}'.", token.kind),
}
}
}
@@ -69,12 +69,12 @@ pub enum SimpleLiteral {
impl SimpleLiteral {
pub fn from_token(token: Token) -> Self {
- match token.variant {
+ match token.kind {
Int(int) => Self::Int(int),
Float(float) => Self::Float(float),
KeywordTrue => Self::Bool(true),
KeywordFalse => Self::Bool(false),
- _ => panic!("Can't create literal from '{:?}'.", token.variant),
+ _ => panic!("Can't create literal from '{:?}'.", token.kind),
}
}
}
@@ -115,7 +115,7 @@ pub struct StrNode {
#[derive(Debug, Clone)]
pub enum StrPart {
Literal(String),
- Embed(Expression)
+ Embed(Expression),
}
#[derive(Debug, Clone)]
diff --git a/src/parse/macros.rs b/src/parse/macros.rs
index 156bdb8..97e0104 100644
--- a/src/parse/macros.rs
+++ b/src/parse/macros.rs
@@ -1,7 +1,7 @@
#[macro_export]
macro_rules! check {
- ($self:ident, $($variant:pat_param)|+) => {
- if let Some(Token {variant: $( $variant )|+, ..}) = $self.tokens.peek() {
+ ($self:ident, $($kind:pat_param)|+) => {
+ if let Some(Token {kind: $( $kind )|+, ..}) = $self.tokens.peek() {
true
} else {
false
@@ -11,15 +11,15 @@ macro_rules! check {
#[macro_export]
macro_rules! consume {
- ($self:ident, $($variant:pat_param)|+) => {
+ ($self:ident, $($kind:pat_param)|+) => {
if let Some(token) = $self.tokens.next() {
- if let Token {variant: $( $variant )|+, ..} = token {
+ if let Token {kind: $( $kind )|+, ..} = token {
Ok(token)
} else {
Err(anyhow!(
// Make a better error message
"Received unexpected token: '{:?}'.",
- token.variant
+ token.kind
))
}
} else {
@@ -31,8 +31,8 @@ macro_rules! consume {
#[macro_export]
macro_rules! consume_if {
- ($self:ident, $($variant:pat_param)|+) => {
- if let Some(Token {variant: $( $variant )|+, ..}) = $self.tokens.peek() {
+ ($self:ident, $($kind:pat_param)|+) => {
+ if let Some(Token {kind: $( $kind )|+, ..}) = $self.tokens.peek() {
Some($self.tokens.next().unwrap())
} else {
None
@@ -42,10 +42,10 @@ macro_rules! consume_if {
#[macro_export]
macro_rules! inner {
- ($token:expr, $variant:path ) => {
- match $token.variant {
- $variant(inner) => inner,
- _ => panic!("Tried getting inner content of incorrect variant."),
+ ($token:expr, $kind:path ) => {
+ match $token.kind {
+ $kind(inner) => inner,
+ _ => panic!("Tried getting inner content of incorrect kind."),
}
};
}
diff --git a/src/parse/parser.rs b/src/parse/parser.rs
index ee83293..8b43a74 100644
--- a/src/parse/parser.rs
+++ b/src/parse/parser.rs
@@ -3,7 +3,7 @@ use super::ast::nodes::{ArrayNode, LoopNode, StrNode, UnaryOperator};
use super::ast::statement::Statement;
use super::ast::Program;
use crate::lex::lexer::Lexer;
-use crate::lex::token::TokenVariant::*;
+use crate::lex::token::TokenKind::*;
use crate::parse::ast::nodes::{
ArrayAccessNode, BinaryOperator, BlockNode, CallNode, ConditionalBlock, FnHeader, FnNode,
IfNode, MemberAccessNode, SimpleLiteral, StrPart, TypedIdentifier,
@@ -35,7 +35,7 @@ impl<T: Iterator<Item = Token>> Parser<T> {
fn statement(&mut self) -> Result<Statement> {
let token = self.tokens.peek().expect("Expected token.");
- match token.variant {
+ match token.kind {
KeywordPrint => self.print_statement(),
KeywordReturn => self.return_statement(),
KeywordBreak => self.break_statement(),
@@ -216,7 +216,7 @@ impl<T: Iterator<Item = Token>> Parser<T> {
let mut left = self.unit_expression()?;
while let Some(token) = consume_if!(self, GroupOpen | ArrayOpen | Dot) {
- match token.variant {
+ match token.kind {
GroupOpen => {
let mut arguments = Vec::new();
@@ -257,7 +257,7 @@ impl<T: Iterator<Item = Token>> Parser<T> {
fn unit_expression(&mut self) -> Result<Expression> {
if let Some(token) = self.tokens.peek() {
- match token.variant {
+ match token.kind {
Int(_) | Float(_) | Str(_) | KeywordTrue | KeywordFalse => {
let literal = self.tokens.next().unwrap();
Ok(Expression::SimpleLiteral(SimpleLiteral::from_token(
@@ -275,7 +275,7 @@ impl<T: Iterator<Item = Token>> Parser<T> {
KeywordFn => Ok(Expression::FnLiteral(Box::new(self.function()?))),
KeywordIf => Ok(Expression::If(Box::new(self.conditional()?))),
KeywordLoop => Ok(Expression::Loop(Box::new(self.repeating()?))),
- _ => Err(anyhow!("Unexpected token: {:?}", token.variant)),
+ _ => Err(anyhow!("Unexpected token: {:?}", token.kind)),
}
} else {
Err(anyhow!("Expected expression."))
@@ -313,7 +313,7 @@ impl<T: Iterator<Item = Token>> Parser<T> {
loop {
let token = self.tokens.next().expect("Unclosed str.");
- let part = match token.variant {
+ let part = match token.kind {
Str(literal) => StrPart::Literal(literal),
StrEmbed(code) => {
let embed_lexer = Lexer::new(&code);
@@ -440,7 +440,7 @@ impl<T: Iterator<Item = Token>> Parser<T> {
loop {
let token = self.tokens.peek().expect("Unclosed block.");
- match token.variant {
+ match token.kind {
KeywordReturn | KeywordPrint | KeywordContinue | KeywordBreak => {
statements.push(self.statement()?);
}
diff --git a/src/types/bag.rs b/src/types/bag.rs
index 30202fd..1b2d891 100644
--- a/src/types/bag.rs
+++ b/src/types/bag.rs
@@ -1,6 +1,6 @@
use std::{cell::RefCell, collections::HashMap};
-use super::{RefValueMap, Type, TypeVariant};
+use super::{RefValueMap, Type, TypeKind};
#[derive(Clone)]
pub struct TypeBag {
@@ -21,22 +21,20 @@ impl TypeBag {
}
}
- pub fn create_type(&self, variant: TypeVariant) -> Type {
- let global_associated_values_for_type = match variant {
- TypeVariant::Fn { .. } => &self.global_associated_values.fn_values,
- TypeVariant::Array(_) => &self.global_associated_values.array_values,
- TypeVariant::Tuple(_) => &self.global_associated_values.tuple_values,
- TypeVariant::Data { .. } => &self.global_associated_values.data_values,
- TypeVariant::Str
- | TypeVariant::Int
- | TypeVariant::Float
- | TypeVariant::Bool
- | TypeVariant::Void => panic!("There should only be one type {:?}.", variant),
- TypeVariant::Face(_) | TypeVariant::Var(_) => todo!("Implement abstract types."),
+ pub fn create_type(&self, kind: TypeKind) -> Type {
+ let global_associated_values_for_type = match kind {
+ TypeKind::Fn { .. } => &self.global_associated_values.fn_values,
+ TypeKind::Array(_) => &self.global_associated_values.array_values,
+ TypeKind::Tuple(_) => &self.global_associated_values.tuple_values,
+ TypeKind::Data { .. } => &self.global_associated_values.data_values,
+ TypeKind::Str | TypeKind::Int | TypeKind::Float | TypeKind::Bool | TypeKind::Void => {
+ panic!("There should only be one type {:?}.", kind)
+ }
+ TypeKind::Face(_) | TypeKind::Var(_) => todo!("Implement abstract types."),
};
Type {
- variant: Box::new(variant),
+ kind: Box::new(kind),
global_associated_values: global_associated_values_for_type,
}
}
@@ -106,17 +104,17 @@ struct AtomicTypes {
impl AtomicTypes {
fn new(values: &'static GlobalAssociatedValues) -> Self {
AtomicTypes {
- str_type: Self::create_single_type(TypeVariant::Str, &values.str_values),
- int_type: Self::create_single_type(TypeVariant::Int, &values.int_values),
- float_type: Self::create_single_type(TypeVariant::Float, &values.float_values),
- bool_type: Self::create_single_type(TypeVariant::Bool, &values.bool_values),
- void_type: Self::create_single_type(TypeVariant::Void, &values.void_values),
+ str_type: Self::create_single_type(TypeKind::Str, &values.str_values),
+ int_type: Self::create_single_type(TypeKind::Int, &values.int_values),
+ float_type: Self::create_single_type(TypeKind::Float, &values.float_values),
+ bool_type: Self::create_single_type(TypeKind::Bool, &values.bool_values),
+ void_type: Self::create_single_type(TypeKind::Void, &values.void_values),
}
}
- fn create_single_type(variant: TypeVariant, values: &'static RefValueMap) -> Type {
+ fn create_single_type(kind: TypeKind, values: &'static RefValueMap) -> Type {
Type {
- variant: Box::new(variant),
+ kind: Box::new(kind),
global_associated_values: values,
}
}
diff --git a/src/types/mod.rs b/src/types/mod.rs
index f3257eb..ecea578 100644
--- a/src/types/mod.rs
+++ b/src/types/mod.rs
@@ -8,12 +8,12 @@ use crate::parse::ast::nodes::Identifier;
#[derive(Debug, Clone)]
pub struct Type {
- variant: Box<TypeVariant>,
+ kind: Box<TypeKind>,
global_associated_values: &'static RefValueMap,
}
#[derive(Debug, Clone)]
-pub enum TypeVariant {
+pub enum TypeKind {
// Concrete types
Str,
Int,
@@ -39,4 +39,4 @@ pub enum TypeVariant {
type TypeMap = HashMap<Identifier, Type>;
-type RefValueMap = RefCell<ValueMap>; \ No newline at end of file
+type RefValueMap = RefCell<ValueMap>;