Skip to content

Commit

Permalink
Further replacing token indexes with Span
Browse files Browse the repository at this point in the history
  • Loading branch information
VonTum committed Feb 27, 2024
1 parent f6695a6 commit 9013803
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 95 deletions.
12 changes: 6 additions & 6 deletions src/ast.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@


use crate::{errors::ErrorCollector, file_position::{BracketSpan, Span}, flattening::FlattenedModule, instantiation::InstantiationList, linker::FileUUID, tokenizer::{get_token_type_name, TokenTypeIdx}, value::Value};
use crate::{errors::ErrorCollector, file_position::{BracketSpan, SingleCharSpan, Span}, flattening::FlattenedModule, instantiation::InstantiationList, linker::FileUUID, tokenizer::{get_token_type_name, TokenTypeIdx}, value::Value};
use core::ops::Range;
use std::fmt::Display;

Expand Down Expand Up @@ -67,14 +67,14 @@ pub struct Identifier {
pub enum Expression {
Named(Identifier),
Constant(Value),
UnaryOp(Box<(Operator, usize/*Operator token */, SpanExpression)>),
BinOp(Box<(SpanExpression, Operator, usize/*Operator token */, SpanExpression)>),
UnaryOp(Box<(Operator, Span/*Operator token */, SpanExpression)>),
BinOp(Box<(SpanExpression, Operator, Span/*Operator token */, SpanExpression)>),
Array(Box<(SpanExpression, SpanExpression, BracketSpan)>), // first[second]
FuncCall(Vec<SpanExpression>) // first(second, third, ...)
}

impl Expression {
pub fn new_binop(left : SpanExpression, op : Operator, op_pos : usize/*Operator token */, right : SpanExpression) -> SpanExpression {
pub fn new_binop(left : SpanExpression, op : Operator, op_pos : Span/*Operator token */, right : SpanExpression) -> SpanExpression {
let span = Span::new_overarching(left.1, right.1);
(Expression::BinOp(Box::new((left, op, op_pos, right))), span)
}
Expand All @@ -91,7 +91,7 @@ pub enum LeftExpression {
#[derive(Debug)]
pub enum AssignableExpressionModifiers {
LatencyAdding{num_regs : i64, regs_span : Span},
Initial{initial_token : usize},
Initial{initial_token : Span},
NoModifiers
}

Expand All @@ -110,7 +110,7 @@ pub struct RangeExpression {

#[derive(Debug)]
pub enum Statement {
Assign{to : Vec<AssignableExpressionWithModifiers>, eq_sign_position : Option<usize>, expr : Option<SpanExpression>}, // num_regs v = expr;
Assign{to : Vec<AssignableExpressionWithModifiers>, eq_sign_position : Option<SingleCharSpan>, expr : Option<SpanExpression>}, // num_regs v = expr;
If{condition : SpanExpression, then : CodeBlock, els : Option<CodeBlock>},
For{var : SignalDeclaration, range : RangeExpression, code : CodeBlock},
Block(CodeBlock)
Expand Down
6 changes: 6 additions & 0 deletions src/file_position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ impl Into<Span> for SingleCharSpan {
}
}

impl Into<Span> for &SingleCharSpan {
fn into(self) -> Span {
Span(self.char_token, self.char_token)
}
}

#[derive(Clone, Copy, PartialEq, Eq)]
pub struct CharLine {
pub line : usize,
Expand Down
2 changes: 1 addition & 1 deletion src/flattening/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ impl<'prev, 'inst, 'l, 'runtime> FlatteningContext<'prev, 'inst, 'l, 'runtime> {
let excess_results_span = Span::new_overarching(to[num_func_outputs].span, *func_span).dont_include_last_token();
self.errors.error_with_info(excess_results_span, format!("Excess output targets. Function returns {num_func_outputs} results, but {num_targets} targets were given."), info);
} else {
let too_few_targets_pos = if let Some(eq) = eq_sign_position {Span::new_single_token(*eq)} else {func_name_span};
let too_few_targets_pos = if let Some(eq) = eq_sign_position {eq.into()} else {func_name_span};
self.errors.error_with_info(too_few_targets_pos, format!("Too few output targets. Function returns {num_func_outputs} results, but {num_targets} targets were given."), info);
}
}
Expand Down
Loading

0 comments on commit 9013803

Please sign in to comment.