Skip to content

Commit

Permalink
Encountered globals and types are now stored as part of AST
Browse files Browse the repository at this point in the history
  • Loading branch information
VonTum committed Aug 24, 2023
1 parent 4b24f6b commit 2dd591b
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 77 deletions.
34 changes: 26 additions & 8 deletions multiply_add.sus
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ module parallel_mul_add_reg : int a, int b -> int p, int q {
Lööwe 老虎;
}


/*
a
b
c
tmp
result
*/

timeline (v v _ -> _) .. (_ _ v -> v)
module mul_add : int a, int b, int c -> int result {
reg int tmp = a * b;
result = tmp + c;
}




/* a module to test the syntax */
module MultiplyAdd : i32 a, i32 b, i32 c -> i32 result {
// temporary variable
Expand Down Expand Up @@ -67,6 +85,14 @@ module beep : i32 a {

}

timeline (a -> r) .. (/ -> r)
module dwiogo : bool[512] data -> bool[256] out {
state bool[256] save = data[256:511];
out = data[0:255];
#
out = save;
}


module multiply_add : i32 a, i32 b, i32 c -> i32 result, double double_result {
i32 tmp = a * b;
Expand Down Expand Up @@ -98,14 +124,6 @@ module my_complex_operation : i32'0 a -> i32'9 result {
}
}

timeline (a -> r) .. (/ -> r)
module dwiogo : bool[512] data -> bool[256] out {
state bool[256] save = data[256:511];
out = data[0:255];
#
out = save;
}

timeline (a -> /)* .. (/ -> r)
module seq_adder : i32 a -> i32 result {
state int sum = a;
Expand Down
87 changes: 45 additions & 42 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use num_bigint::BigUint;

use crate::tokenizer::{TokenTypeIdx, TokenExtraInfo};
use core::ops::Range;
use std::ops::{Sub, Add};

// Token span. Indices are INCLUSIVE
#[derive(Clone,Copy,Debug,PartialEq,Eq)]
Expand All @@ -16,6 +17,27 @@ impl Span {
}
}

// This type describes a subrange within a larger range.
// It is meant to be used to save spans within the ast, so only their outer span needs to be moved
#[derive(Clone,Debug,PartialEq,Eq,Hash)]
pub struct RelativeRange<T : PartialOrd + Sub + Add + Copy>(Range<T>);
impl<T : PartialOrd + Sub<Output = T> + Add<Output = T> + Copy> RelativeRange<T> {
pub fn new_subrange(outer : Range<T>, inner : Range<T>) -> Self {
assert!(outer.start <= inner.start);
assert!(outer.end >= inner.end);
RelativeRange(inner.start - outer.start .. inner.end - outer.end)
}
pub fn as_range(&self, outer : Range<T>) -> Range<T> {
// This range falls within the bounds of the outer range
let real_range = outer.start + self.0.start .. outer.start + self.0.end;

assert!(real_range.start <= outer.end);
assert!(real_range.end <= outer.end);

real_range
}
}

#[derive(Debug,Clone,Copy,PartialEq,Eq)]
pub struct FilePos {
pub char_idx : usize, // Byte index
Expand All @@ -27,11 +49,10 @@ pub struct FilePos {
#[derive(Clone,Copy,Debug,PartialEq,Eq)]
pub struct CharSpan{
pub file_pos : FilePos,
pub length : usize // in bytes. Can just do file_text[file_pos.char_idx .. file_pos.char_idx = length]
pub length : usize // in bytes. Can just do file_text[file_pos.char_idx .. file_pos.char_idx + length]
}



pub fn cvt_span_to_char_span(sp : Span, char_sp_buf : &[CharSpan]) -> CharSpan {
let file_pos = char_sp_buf[if sp.0 < char_sp_buf.len() {sp.0} else {char_sp_buf.len()-1}].file_pos;
let length = char_sp_buf[if sp.1 < char_sp_buf.len() {sp.1} else {char_sp_buf.len()-1}].end_pos() - file_pos.char_idx;
Expand Down Expand Up @@ -73,34 +94,12 @@ impl From<usize> for Span {
}
}

const GLOBAL_IDENTIFIER_OFFSET : TokenExtraInfo = 1 << (TokenExtraInfo::BITS - 1);
#[derive(Debug, Clone, Copy)]
pub struct IdentifierIdx {
name_idx : TokenExtraInfo
pub enum LocalOrGlobal {
Local(usize),
Global(usize)
}

impl IdentifierIdx {
pub fn new_local(local_idx : usize) -> IdentifierIdx {
IdentifierIdx{name_idx : local_idx as TokenExtraInfo}
}
pub fn new_global(global_idx : TokenExtraInfo) -> IdentifierIdx {
IdentifierIdx{name_idx : global_idx + GLOBAL_IDENTIFIER_OFFSET}
}
pub fn get_local(&self) -> Option<usize> {
if self.name_idx < GLOBAL_IDENTIFIER_OFFSET {
Some(self.name_idx as usize)
} else {
None
}
}
pub fn get_global(&self) -> Option<TokenExtraInfo> {
if self.name_idx >= GLOBAL_IDENTIFIER_OFFSET {
Some((self.name_idx - GLOBAL_IDENTIFIER_OFFSET) as TokenExtraInfo)
} else {
None
}
}
}

#[derive(Debug, Clone, Copy)]
pub struct IdentifierToken {
Expand All @@ -111,7 +110,7 @@ pub struct IdentifierToken {

#[derive(Debug, Clone)]
pub enum TypeExpression {
Named(u64),
Named(GlobalPart),
Array(Box<(SpanTypeExpression, SpanExpression)>)
}

Expand All @@ -138,7 +137,7 @@ pub enum Value {

#[derive(Debug,Clone)]
pub enum Expression {
Named(IdentifierIdx),
Named(LocalOrGlobal),
Constant(Value),
UnaryOp(Box<(Operator, usize/*Operator token */, SpanExpression)>),
BinOp(Box<(SpanExpression, Operator, usize/*Operator token */, SpanExpression)>),
Expand Down Expand Up @@ -177,18 +176,22 @@ pub struct Module {
pub code : Vec<SpanStatement>
}

pub type GlobalPart = usize;

#[derive(Debug)]
pub struct ASTRoot {
pub modules : Vec<Module>
pub modules : Vec<Module>,
pub global_references : Vec<GlobalPart>,
pub type_references : Vec<GlobalPart>
}

pub trait IterIdentifiers {
fn for_each_value<F>(&self, func : &mut F) where F : FnMut(IdentifierIdx, usize) -> ();
fn for_each_type<F>(&self, func : &mut F) where F : FnMut(TokenExtraInfo, usize) -> ();
fn for_each_value<F>(&self, func : &mut F) where F : FnMut(LocalOrGlobal, usize) -> ();
fn for_each_type<F>(&self, func : &mut F) where F : FnMut(GlobalPart, usize) -> ();
}

impl IterIdentifiers for SpanExpression {
fn for_each_value<F>(&self, func : &mut F) where F : FnMut(IdentifierIdx, usize) -> () {
fn for_each_value<F>(&self, func : &mut F) where F : FnMut(LocalOrGlobal, usize) -> () {
let (expr, span) = self;
match expr {
Expression::Named(id) => {
Expand Down Expand Up @@ -217,16 +220,16 @@ impl IterIdentifiers for SpanExpression {
}
}
}
fn for_each_type<F>(&self, _func : &mut F) where F : FnMut(TokenExtraInfo, usize) -> () {}
fn for_each_type<F>(&self, _func : &mut F) where F : FnMut(GlobalPart, usize) -> () {}
}

impl IterIdentifiers for SpanAssignableExpression {
fn for_each_value<F>(&self, func : &mut F) where F : FnMut(IdentifierIdx, usize) -> () {
fn for_each_value<F>(&self, func : &mut F) where F : FnMut(LocalOrGlobal, usize) -> () {
let (expr, span) = self;
match expr {
AssignableExpression::Named{local_idx: id, num_regs : _} => {
assert!(span.0 == span.1);
func(IdentifierIdx::new_local(*id), span.0)
func(LocalOrGlobal::Local(*id), span.0)
}
AssignableExpression::ArrayIndex(b) => {
let (array, idx) = &**b;
Expand All @@ -235,11 +238,11 @@ impl IterIdentifiers for SpanAssignableExpression {
}
}
}
fn for_each_type<F>(&self, _func : &mut F) where F : FnMut(TokenExtraInfo, usize) -> () {}
fn for_each_type<F>(&self, _func : &mut F) where F : FnMut(GlobalPart, usize) -> () {}
}

impl IterIdentifiers for SpanTypeExpression {
fn for_each_value<F>(&self, func : &mut F) where F : FnMut(IdentifierIdx, usize) -> () {
fn for_each_value<F>(&self, func : &mut F) where F : FnMut(LocalOrGlobal, usize) -> () {
let (typ, _span) = self;
match typ {
TypeExpression::Named(_n) => {
Expand All @@ -252,7 +255,7 @@ impl IterIdentifiers for SpanTypeExpression {
}
}
}
fn for_each_type<F>(&self, func : &mut F) where F : FnMut(TokenExtraInfo, usize) -> () {
fn for_each_type<F>(&self, func : &mut F) where F : FnMut(GlobalPart, usize) -> () {
let (typ, span) = self;
match typ {
TypeExpression::Named(n) => {
Expand Down Expand Up @@ -282,9 +285,9 @@ pub fn for_each_assign_in_block<F>(block : &Vec<SpanStatement>, func : &mut F) w
}

impl IterIdentifiers for Module {
fn for_each_value<F>(&self, func : &mut F) where F : FnMut(IdentifierIdx, usize) -> () {
fn for_each_value<F>(&self, func : &mut F) where F : FnMut(LocalOrGlobal, usize) -> () {
for (pos, decl) in self.declarations.iter().enumerate() {
func(IdentifierIdx::new_local(pos), decl.span.1);
func(LocalOrGlobal::Local(pos), decl.span.1);
}
for_each_assign_in_block(&self.code, &mut |to, v| {
for assign_to in to {
Expand All @@ -293,7 +296,7 @@ impl IterIdentifiers for Module {
v.for_each_value(func);
});
}
fn for_each_type<F>(&self, func : &mut F) where F : FnMut(TokenExtraInfo, usize) -> () {
fn for_each_type<F>(&self, func : &mut F) where F : FnMut(GlobalPart, usize) -> () {
for decl in &self.declarations {
decl.typ.for_each_type(func);
}
Expand Down
20 changes: 10 additions & 10 deletions src/code_generation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ use crate::{ast::*, errors::ParsingError};
#[derive(Debug)]
pub enum Assignable {
Named{local_idx : usize, num_regs : usize},
Array{to : Box<Assignable>, value : IdentifierIdx}
Array{to : Box<Assignable>, value : LocalOrGlobal}
}

#[derive(Debug)]
pub enum Operation {
BinaryOp{out : Assignable, left : IdentifierIdx, op : Operator, right : IdentifierIdx},
UnaryOp{out : Assignable, op : Operator, right : IdentifierIdx},
Copy{out : Assignable, input : IdentifierIdx},
BinaryOp{out : Assignable, left : LocalOrGlobal, op : Operator, right : LocalOrGlobal},
UnaryOp{out : Assignable, op : Operator, right : LocalOrGlobal},
Copy{out : Assignable, input : LocalOrGlobal},
Constant{out : Assignable, val : Value},
FunctionCall{results : Vec<Assignable>, func_name : IdentifierIdx, args : Vec<IdentifierIdx>},
ArrayAccess{result : Assignable, array : IdentifierIdx, args : Vec<IdentifierIdx>}
FunctionCall{results : Vec<Assignable>, func_name : LocalOrGlobal, args : Vec<LocalOrGlobal>},
ArrayAccess{result : Assignable, array : LocalOrGlobal, args : Vec<LocalOrGlobal>}
}

#[derive(Debug)]
Expand All @@ -38,7 +38,7 @@ impl Flattened {
self.variables.push(LocalVar{span, typ : None, identifier_type : IdentifierType::Local});
new_tmp_id
}
fn flatten_expression(&mut self, (expr, span) : &SpanExpression) -> IdentifierIdx {
fn flatten_expression(&mut self, (expr, span) : &SpanExpression) -> LocalOrGlobal {
match expr {
Expression::Named(n) => {
*n
Expand All @@ -52,7 +52,7 @@ impl Flattened {

self.operations.push((Operation::BinaryOp { out: Assignable::Named{local_idx : new_idx, num_regs : 0}, left: left_id, op: *op, right: right_id }, Span::from(*op_pos)));

IdentifierIdx::new_local(new_idx)
LocalOrGlobal::Local(new_idx)
},
Expression::UnaryOp(b) => {
let (op, op_pos, right) = &**b;
Expand All @@ -62,12 +62,12 @@ impl Flattened {

self.operations.push((Operation::UnaryOp { out: Assignable::Named{local_idx : new_idx, num_regs : 0}, op: *op, right: right_id }, Span::from(*op_pos)));

IdentifierIdx::new_local(new_idx)
LocalOrGlobal::Local(new_idx)
},
Expression::Constant(cst) => {
let tmp_local = self.new_local(*span);
self.operations.push((Operation::Constant { out: Assignable::Named{local_idx : tmp_local, num_regs : 0}, val: cst.clone() }, *span));
IdentifierIdx::new_local(tmp_local)
LocalOrGlobal::Local(tmp_local)
},
Expression::FuncCall(args) => {
/*let mut arg_iter = args.iter();
Expand Down
2 changes: 1 addition & 1 deletion src/dev_aid/syntax_highlighting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ fn walk_name_color(ast : &ASTRoot, result : &mut [IDEToken]) {
result[position].typ = IDETokenType::Identifier(IDEIdentifierType::Type);
});
module.for_each_value(&mut |name, position| {
result[position].typ = IDETokenType::Identifier(if let Some(l) = name.get_local() {
result[position].typ = IDETokenType::Identifier(if let LocalOrGlobal::Local(l) = name {
IDEIdentifierType::Value(module.declarations[l].identifier_type)
} else {
IDEIdentifierType::Unknown
Expand Down
Loading

0 comments on commit 2dd591b

Please sign in to comment.