Skip to content

Commit

Permalink
Use kind id instead of file text for operator construction
Browse files Browse the repository at this point in the history
  • Loading branch information
VonTum committed Apr 10, 2024
1 parent 330a4d0 commit d693030
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 39 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ repository = "https://github.com/pc2/sus-compiler"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[workspace]
members = ["sus_proc_macro"]

[dependencies]
# console = "0.15.7" # for terminal colors
# chumsky = "0.9.2"
Expand Down
77 changes: 41 additions & 36 deletions src/flattening/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ use std::{iter::zip, str::FromStr};
use num::BigInt;
use sus_proc_macro::{field, kind, kw};
use crate::{
arena_alloc::{ArenaAllocator, FlatAlloc, UUIDMarker, UUIDRange, UUID}, errors::{error_info, ErrorCollector, ErrorInfo}, file_position::{BracketSpan, Span}, instantiation::InstantiationList, linker::{ConstantUUID, FileUUID, GlobalResolver, LinkInfo, Linker, ModuleUUID, NameElem, NamedConstant, NamedType, ResolvedGlobals, ResolvedNameElem, TypeUUIDMarker}, parser::{Cursor, Documentation}, typing::{get_binary_operator_types, typecheck, typecheck_is_array_indexer, typecheck_unary_operator, Type, WrittenType, BOOL_TYPE, INT_TYPE}, value::Value
arena_alloc::{ArenaAllocator, FlatAlloc, UUIDMarker, UUIDRange, UUID},
errors::{error_info, ErrorCollector, ErrorInfo},
file_position::{BracketSpan, Span},
instantiation::InstantiationList,
linker::{ConstantUUID, FileUUID, GlobalResolver, LinkInfo, Linker, ModuleUUID, NameElem, NamedConstant, NamedType, ResolvedGlobals, ResolvedNameElem, TypeUUIDMarker},
parser::{Cursor, Documentation},
typing::{get_binary_operator_types, typecheck, typecheck_is_array_indexer, typecheck_unary_operator, Type, WrittenType, BOOL_TYPE, INT_TYPE},
value::Value
};

use self::name_context::LocalVariableContext;
Expand Down Expand Up @@ -156,15 +163,15 @@ pub enum UnaryOperator {
Negate,
}
impl UnaryOperator {
pub fn from_text(op_text : &str) -> Self {
match op_text {
"+" => UnaryOperator::Sum,
"*" => UnaryOperator::Product,
"-" => UnaryOperator::Negate,
"&" => UnaryOperator::And,
"|" => UnaryOperator::Or,
"^" => UnaryOperator::Xor,
"!" => UnaryOperator::Not,
pub fn from_kind_id(kind_id : u16) -> Self {
match kind_id {
kw!("+") => UnaryOperator::Sum,
kw!("*") => UnaryOperator::Product,
kw!("-") => UnaryOperator::Negate,
kw!("&") => UnaryOperator::And,
kw!("|") => UnaryOperator::Or,
kw!("^") => UnaryOperator::Xor,
kw!("!") => UnaryOperator::Not,
_ => unreachable!()
}
}
Expand Down Expand Up @@ -192,8 +199,8 @@ pub enum BinaryOperator {
Or,
Xor,
Add,
ShiftLeft,
ShiftRight,
//ShiftLeft,
//ShiftRight,
Subtract,
Multiply,
Divide,
Expand All @@ -206,24 +213,24 @@ pub enum BinaryOperator {
LesserEq
}
impl BinaryOperator {
pub fn from_text(op_text : &str) -> Self {
match op_text {
"&" => BinaryOperator::And,
"|" => BinaryOperator::Or,
"^" => BinaryOperator::Xor,
"<<" => BinaryOperator::ShiftLeft,
">>" => BinaryOperator::ShiftRight,
"+" => BinaryOperator::Add,
"-" => BinaryOperator::Subtract,
"*" => BinaryOperator::Multiply,
"/" => BinaryOperator::Divide,
"%" => BinaryOperator::Modulo,
"==" => BinaryOperator::Equals,
"!=" => BinaryOperator::NotEquals,
">" => BinaryOperator::Greater,
">=" => BinaryOperator::GreaterEq,
"<" => BinaryOperator::Lesser,
"<=" => BinaryOperator::LesserEq,
pub fn from_kind_id(kind_id : u16) -> Self {
match kind_id {
kw!("&") => BinaryOperator::And,
kw!("|") => BinaryOperator::Or,
kw!("^") => BinaryOperator::Xor,
//kw!("<<") => BinaryOperator::ShiftLeft,
//kw!(">>") => BinaryOperator::ShiftRight,
kw!("+") => BinaryOperator::Add,
kw!("-") => BinaryOperator::Subtract,
kw!("*") => BinaryOperator::Multiply,
kw!("/") => BinaryOperator::Divide,
kw!("%") => BinaryOperator::Modulo,
kw!("==") => BinaryOperator::Equals,
kw!("!=") => BinaryOperator::NotEquals,
kw!(">") => BinaryOperator::Greater,
kw!(">=") => BinaryOperator::GreaterEq,
kw!("<") => BinaryOperator::Lesser,
kw!("<=") => BinaryOperator::LesserEq,
_ => unreachable!()
}
}
Expand All @@ -232,8 +239,8 @@ impl BinaryOperator {
BinaryOperator::And => "&",
BinaryOperator::Or => "|",
BinaryOperator::Xor => "^",
BinaryOperator::ShiftLeft => "<<",
BinaryOperator::ShiftRight => ">>",
//BinaryOperator::ShiftLeft => "<<",
//BinaryOperator::ShiftRight => ">>",
BinaryOperator::Add => "+",
BinaryOperator::Subtract => "-",
BinaryOperator::Multiply => "*",
Expand Down Expand Up @@ -659,8 +666,7 @@ impl<'l> FlatteningContext<'l> {
} else if kind == kind!("unary_op") {
cursor.go_down_no_check(|cursor| {
cursor.field(field!("operator"));
let op_text = &self.linker.file.file_text[cursor.span()];
let op = UnaryOperator::from_text(op_text);
let op = UnaryOperator::from_kind_id(cursor.kind());

cursor.field(field!("right"));
let right = self.flatten_expr_tree(cursor);
Expand All @@ -673,8 +679,7 @@ impl<'l> FlatteningContext<'l> {
let left = self.flatten_expr_tree(cursor);

cursor.field(field!("operator"));
let op_text = &self.linker.file.file_text[cursor.span()];
let op = BinaryOperator::from_text(op_text);
let op = BinaryOperator::from_kind_id(cursor.kind());

cursor.field(field!("right"));
let right = self.flatten_expr_tree(cursor);
Expand Down
1 change: 0 additions & 1 deletion src/typing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ pub fn get_binary_operator_types(op : BinaryOperator) -> ((Type, Type), Type) {
BinaryOperator::Greater => ((INT_TYPE, INT_TYPE), BOOL_TYPE),
BinaryOperator::LesserEq => ((INT_TYPE, INT_TYPE), BOOL_TYPE),
BinaryOperator::Lesser => ((INT_TYPE, INT_TYPE), BOOL_TYPE),
_ => unreachable!()
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ pub fn compute_binary_op(left : &Value, op : BinaryOperator, right : &Value) ->
BinaryOperator::And => Value::Bool(left.extract_bool() & right.extract_bool()),
BinaryOperator::Or => Value::Bool(left.extract_bool() & right.extract_bool()),
BinaryOperator::Xor => Value::Bool(left.extract_bool() & right.extract_bool()),
BinaryOperator::ShiftLeft => todo!(), // Still a bit iffy about shift operator inclusion
BinaryOperator::ShiftRight => todo!()
//BinaryOperator::ShiftLeft => todo!(), // Still a bit iffy about shift operator inclusion
//BinaryOperator::ShiftRight => todo!()
}
}

0 comments on commit d693030

Please sign in to comment.