Skip to content

Commit

Permalink
Continued compiler work
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert-M-Lucas committed Jun 5, 2024
1 parent 1dd5644 commit 2a22044
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 15 deletions.
31 changes: 28 additions & 3 deletions .idea/workspace.xml

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

2 changes: 1 addition & 1 deletion main.why
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ struct A {
}

fn main() -> int {
let a: int = int::hi;
let a: int = 100 + 100;
let b: int = a;
return b;
}
3 changes: 2 additions & 1 deletion src/root/compiler/compile_evaluable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::root::errors::name_resolver_errors::NRErrors;
use crate::root::errors::WErr;
use crate::root::name_resolver::name_resolvers::{GlobalDefinitionTable, NameResult};
use crate::root::parser::parse_function::parse_evaluable::{EvaluableToken, EvaluableTokens};
use crate::root::parser::parse_function::parse_operator::get_method_name;
use crate::root::shared::common::{FunctionID, Indirection, TypeRef};
use crate::root::shared::common::AddressedTypeRef;

Expand Down Expand Up @@ -84,7 +85,7 @@ pub fn compile_evaluable_into(

t.instantiate_from_literal(target.local_address(), literal)?
}
EvaluableTokens::InfixOperator(_, _, _) => todo!(),
EvaluableTokens::InfixOperator(lhs, op, rhs) => todo!(),
EvaluableTokens::PrefixOperator(_, _) => todo!(),
EvaluableTokens::DynamicAccess(_, _) => todo!(), // Accessed methods must be called
EvaluableTokens::StaticAccess(_, n) => return Err(WErr::n(NRErrors::CannotFindConstantAttribute(n.name().clone()), n.location().clone())), // Accessed methods must be called
Expand Down
2 changes: 1 addition & 1 deletion src/root/errors/name_resolver_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub enum NRErrors {
#[error("Cannot create 'impl' for an indirect type")]
IndirectImpl,
#[error("Cannot find the subname ({0}) of a function ({1})")]
FunctionSubname(String, String),
NoFunctionSubname(String, String),
#[error("Cannot find method ({0}) of type ({1})")]
CannotFindMethod(String, String),
#[error("Two attributes found with the same name ({0})")]
Expand Down
27 changes: 18 additions & 9 deletions src/root/parser/parse_function/parse_operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,34 @@ use nom_supreme::error::GenericErrorTree;
use nom_supreme::tag::complete::tag;
use nom_supreme::tag::TagError;

const OPERATOR_MAPS: [(&str, OperatorTokens, bool); 4] = [
("+", OperatorTokens::Add, false),
("-", OperatorTokens::Subtract, false),
("==", OperatorTokens::Equals, false),
("!", OperatorTokens::Not, true),
const OPERATOR_MAPS: [(&str, OperatorTokens, bool, &'static str); 4] = [
("+", OperatorTokens::Add, false, "add"),
("-", OperatorTokens::Subtract, false, "sub"),
("==", OperatorTokens::Equals, false, "eq"),
("!", OperatorTokens::Not, true, "not"),
];

// TODO: Implement functionally
pub fn is_prefix_op(operator: &OperatorTokens) -> bool {
for (_, op, prefix) in &OPERATOR_MAPS {
for (_, op, prefix, _) in &OPERATOR_MAPS {
if operator == op {
return *prefix;
}
}
panic!()
}

pub fn get_method_name(operator: &OperatorTokens) -> &'static str {
for (_, op, _, name) in &OPERATOR_MAPS {
if operator == op {
return *name;
}
}
panic!()
}

pub fn get_priority(operator: &OperatorTokens) -> usize {
for (p, (_, op, _)) in OPERATOR_MAPS.iter().enumerate() {
for (p, (_, op, _, _)) in OPERATOR_MAPS.iter().enumerate() {
if operator == op {
return p;
}
Expand Down Expand Up @@ -68,7 +77,7 @@ impl OperatorTokens {
}

pub fn parse_operator(s: Span) -> ParseResult<Span, OperatorToken> {
for (operator, token, _) in OPERATOR_MAPS {
for (operator, token, _, _) in OPERATOR_MAPS {
if let Ok((s, x)) = tag::<_, _, ErrorTree>(operator)(s) {
return Ok((
s,
Expand All @@ -83,7 +92,7 @@ pub fn parse_operator(s: Span) -> ParseResult<Span, OperatorToken> {
Err(Error(GenericErrorTree::Alt(
OPERATOR_MAPS
.iter()
.map(|(t, _, _)| GenericErrorTree::from_tag(s, *t))
.map(|(t, _, _, _)| GenericErrorTree::from_tag(s, *t))
.collect(),
)))
}

0 comments on commit 2a22044

Please sign in to comment.