Skip to content

Commit

Permalink
Work on typing
Browse files Browse the repository at this point in the history
  • Loading branch information
VonTum committed Nov 7, 2023
1 parent 1f4ff01 commit 713f46b
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 90 deletions.
8 changes: 8 additions & 0 deletions src/arena_alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ impl<IndexMarker> Default for UUID<IndexMarker> {

impl<IndexMarker> UUID<IndexMarker> {
pub const INVALID : Self = UUID(usize::MAX, PhantomData);

pub const fn from_hidden_value(v : usize) -> Self {
UUID(v, PhantomData)
}

pub const fn get_hidden_value(&self) -> usize {
self.0
}
}

#[derive(Default)]
Expand Down
52 changes: 32 additions & 20 deletions src/ast.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

use num::bigint::BigUint;

use crate::{tokenizer::TokenTypeIdx, linker::{ValueUUID, FileUUID}, flattening::FlattenedModule};
use crate::{tokenizer::TokenTypeIdx, linker::{NamedUUID, FileUUID}, flattening::FlattenedModule};
use core::ops::Range;
use std::ops::Deref;

Expand Down Expand Up @@ -51,22 +51,40 @@ pub enum LocalOrGlobal {
#[derive(Debug, Clone)]
pub enum TypeExpression {
Named(usize), // position in referenced globals list
Array(Box<(SpanTypeExpression, SpanExpression)>)
Array(Box<(TypeExpression, SpanExpression)>)
}

impl TypeExpression {
pub fn get_root(&self) -> usize {
match self {
Self::Named(s) => *s,
Self::Array(b) => {
b.deref().0.0.get_root()
b.deref().0.get_root()
}
}
}
pub fn map_to_type<F : Fn(usize) -> NamedUUID>(&self, f : F) -> Type {
match self {
TypeExpression::Named(n) => Type::Named(f(*n)),
TypeExpression::Array(b) => {
let (sub, idx) = b.deref();
Type::Array(Box::new(sub.map_to_type(f)))
// TODO gather bound constraints
},
}
}
}

pub type SpanTypeExpression = (TypeExpression, Span);

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Type {
Named(NamedUUID),
Array(Box<Type>)
}

pub type SpanType = (Type, Span);

#[derive(Debug,Clone)]
pub struct SignalDeclaration {
pub span : Span,
Expand Down Expand Up @@ -123,9 +141,15 @@ pub struct CodeBlock {
pub statements : Vec<SpanStatement>
}

#[derive(Debug)]
pub struct Bound {
max : SpanExpression
}

#[derive(Debug)]
pub enum Statement {
Declaration{local_id : usize},
AssumeBound{to : SpanAssignableExpression, bound : Bound},
Assign{to : Vec<AssignableExpressionWithModifiers>, eq_sign_position : Option<usize>, expr : SpanExpression}, // num_regs v = expr;
If{condition : SpanExpression, then : CodeBlock, els : Option<CodeBlock>},
Block(CodeBlock),
Expand Down Expand Up @@ -200,7 +224,7 @@ impl Module {
}

#[derive(Debug,Clone,Copy)]
pub struct GlobalReference(pub Span, pub ValueUUID); // token index, and name span
pub struct GlobalReference(pub Span, pub NamedUUID); // token index, and name span

#[derive(Debug)]
pub struct ASTRoot {
Expand Down Expand Up @@ -260,26 +284,14 @@ impl IterIdentifiers for SpanAssignableExpression {
}
}

impl IterIdentifiers for SpanTypeExpression {
fn for_each_value<F>(&self, func : &mut F) where F : FnMut(LocalOrGlobal, usize) -> () {
let (typ, _span) = self;
match typ {
TypeExpression::Named(_n) => {
// is type
}
TypeExpression::Array(b) => {
let (arr_typ, arr_size) = &**b;
arr_typ.for_each_value(func);
arr_size.for_each_value(func);
}
}
}
}

impl IterIdentifiers for CodeBlock {
fn for_each_value<F>(&self, func : &mut F) where F : FnMut(LocalOrGlobal, usize) -> () {
for (stmt, _span) in &self.statements {
match stmt {
Statement::AssumeBound{to, bound} => {
to.for_each_value(func);
bound.max.for_each_value(func);
}
Statement::Assign{to, eq_sign_position : _, expr} => {
for assign_to in to {
assign_to.expr.for_each_value(func);
Expand Down
8 changes: 4 additions & 4 deletions src/dev_aid/syntax_highlighting.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

use std::{ops::Range, path::PathBuf};

use crate::{ast::*, tokenizer::*, parser::*, linker::{PreLinker, FileData, Links, ValueUUID, Named, Linkable}, arena_alloc::ArenaVector};
use crate::{ast::*, tokenizer::*, parser::*, linker::{PreLinker, FileData, Links, NamedUUID, Named, Linkable}, arena_alloc::ArenaVector};

use ariadne::FileCache;
use console::Style;
Expand Down Expand Up @@ -108,7 +108,7 @@ impl Named {
}
}

fn walk_name_color(all_objects : &[ValueUUID], links : &Links, result : &mut [IDEToken]) {
fn walk_name_color(all_objects : &[NamedUUID], links : &Links, result : &mut [IDEToken]) {
for obj_uuid in all_objects {
let object = &links.globals[*obj_uuid];
match object {
Expand All @@ -130,7 +130,7 @@ fn walk_name_color(all_objects : &[ValueUUID], links : &Links, result : &mut [ID
result[name_part].typ = IDETokenType::Identifier(ide_typ);
}
for GlobalReference(reference_span, ref_uuid) in &link_info.global_references {
let typ = if *ref_uuid != ValueUUID::INVALID {
let typ = if *ref_uuid != NamedUUID::INVALID {
IDETokenType::Identifier(links.globals[*ref_uuid].get_ide_type())
} else {
IDETokenType::Invalid
Expand Down Expand Up @@ -245,7 +245,7 @@ pub fn syntax_highlight_file(file_paths : Vec<PathBuf>) {
linker.flatten_all_modules_in_file(file_uuid, &mut errors);

for err in errors.errors {
err.pretty_print_error(f.parsing_errors.file, &token_offsets, &paths_arena, &mut file_cache);
//err.pretty_print_error(f.parsing_errors.file, &token_offsets, &paths_arena, &mut file_cache);
}
}
}
30 changes: 21 additions & 9 deletions src/flattening.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::ops::{Deref, Range};

use crate::{
ast::{Span, Value, Module, Expression, SpanExpression, LocalOrGlobal, Operator, AssignableExpression, SpanAssignableExpression, Statement, CodeBlock, AssignableExpressionWithModifiers, TypeExpression},
linker::{ValueUUID, Linker, Named, Linkable},
ast::{Span, Value, Module, Expression, SpanExpression, LocalOrGlobal, Operator, AssignableExpression, SpanAssignableExpression, Statement, CodeBlock, AssignableExpressionWithModifiers, TypeExpression, Type, SpanType},
linker::{NamedUUID, Linker, Named, Linkable},
errors::{ErrorCollector, error_info}, arena_alloc::{ListAllocator, UUID}, tokenizer::kw
};

Expand All @@ -18,7 +18,7 @@ pub type InstantiationID = UUID<InstantiationIDMarker>;
pub enum WireOrInstantiation {
Wire(WireID),
Instantiation(InstantiationID),
Other(ValueUUID)
Other(NamedUUID)
}

#[derive(Debug)]
Expand Down Expand Up @@ -48,7 +48,7 @@ type SpanConnectionRead = (ConnectionRead, Span);

#[derive(Debug)]
pub enum Instantiation {
Named(ValueUUID),
Named(NamedUUID),
UnaryOp(Operator),
BinaryOp(Operator)
}
Expand All @@ -68,7 +68,7 @@ impl Connection {

#[derive(Debug)]
pub struct Wire {
pub typ : Option<TypeExpression>
pub typ : Option<SpanType>
}

struct FlatteningContext<'l, 'm, 'e> {
Expand Down Expand Up @@ -104,7 +104,7 @@ impl<'l, 'm, 'e> FlatteningContext<'l, 'm, 'e> {
}
}
Named::Type(_) => {
WireOrInstantiation::Wire(wires.alloc(Wire{typ : Some(decl.typ.0.clone())}))
WireOrInstantiation::Wire(wires.alloc(Wire{typ : Some((decl.typ.0.map_to_type(|n| module.link_info.global_references[n].1), decl.typ.1))}))
}
};
LocalVariable{location : decl.span, wire_or_instance}
Expand Down Expand Up @@ -257,7 +257,7 @@ impl<'l, 'm, 'e> FlatteningContext<'l, 'm, 'e> {
}
}
fn make_binary_operator_to_new_local(&mut self, op : Operator, left : WireID, left_span : Span, right : WireID, right_span : Span, condition : WireID, output_span : Span) -> WireID {
let instantiation_idx = self.instantiations.alloc(Instantiation::BinaryOp(Operator{op_typ: kw("&")}));
let instantiation_idx = self.instantiations.alloc(Instantiation::BinaryOp(op));
self.connections.push(Connection{
num_regs: 0,
from: (ConnectionRead::Local(left), left_span),
Expand All @@ -280,7 +280,7 @@ impl<'l, 'm, 'e> FlatteningContext<'l, 'm, 'e> {
new_output_wire
}
fn make_unary_operator_to_new_local(&mut self, op : Operator, right : WireID, right_span : Span, condition : WireID, output_span : Span) -> WireID {
let instantiation_idx = self.instantiations.alloc(Instantiation::BinaryOp(Operator{op_typ: kw("&")}));
let instantiation_idx = self.instantiations.alloc(Instantiation::BinaryOp(op));
self.connections.push(Connection{
num_regs: 0,
from: (ConnectionRead::Local(right), right_span),
Expand Down Expand Up @@ -309,6 +309,9 @@ impl<'l, 'm, 'e> FlatteningContext<'l, 'm, 'e> {
Statement::Declaration{local_id} => {
// TODO
}
Statement::AssumeBound{to, bound} => {
// TODO
}
Statement::If{condition : condition_expr, then, els} => {
let Some(flat_inner_condition) = self.flatten_single_expr(condition_expr, condition) else {continue;};
let inner_condition_span = flat_inner_condition.1;
Expand Down Expand Up @@ -366,6 +369,15 @@ impl<'l, 'm, 'e> FlatteningContext<'l, 'm, 'e> {
}
}
}

pub fn get_type(&self, ) {

}
pub fn type_check(&mut self) {
for c in &self.connections {

}
}
}

pub fn flatten(module : &Module, linker : &Linker, errors : &mut ErrorCollector) -> FlattenedModule {
Expand All @@ -387,7 +399,7 @@ pub struct FlattenedModule {

#[derive(Debug)]
struct InstantiatedWire {
typ : TypeExpression,
typ : Type,
latency : i64
}

Expand Down
Loading

0 comments on commit 713f46b

Please sign in to comment.