Skip to content

Commit

Permalink
Refactor, move local decl resolution to Flattening
Browse files Browse the repository at this point in the history
  • Loading branch information
VonTum committed Feb 10, 2024
1 parent 50abbf0 commit 8a8527d
Show file tree
Hide file tree
Showing 8 changed files with 426 additions and 360 deletions.
67 changes: 35 additions & 32 deletions src/ast.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@


use crate::{tokenizer::{get_token_type_name, TokenTypeIdx, TokenizeResult}, linker::FileUUID, flattening::FlattenedModule, arena_alloc::{UUIDMarker, UUID, FlatAlloc}, instantiation::InstantiationList, value::Value, errors::ErrorCollector};
use crate::{errors::ErrorCollector, flattening::FlattenedModule, instantiation::InstantiationList, linker::FileUUID, tokenizer::{get_token_type_name, TokenTypeIdx, TokenizeResult}, value::Value};
use core::ops::Range;
use std::{fmt::Display, iter::zip};


#[derive(Debug,Clone,Copy,PartialEq,Eq,Hash)]
pub struct DeclIDMarker;
impl UUIDMarker for DeclIDMarker {const DISPLAY_NAME : &'static str = "decl_";}
pub type DeclID = UUID<DeclIDMarker>;
use std::fmt::Display;


// Token span. Indices are INCLUSIVE
Expand Down Expand Up @@ -60,6 +54,13 @@ impl Span {
assert!(self.1 == self.0, "Span is not singleton! {}..{}", self.0, self.1);
self.0
}
pub fn is_single_token(&self) -> Option<usize> {
if self.0 == self.1 {
Some(self.0)
} else {
None
}
}
}

impl IntoIterator for Span {
Expand Down Expand Up @@ -99,24 +100,16 @@ impl IdentifierType {
}
}

#[derive(Debug, Clone, Copy)]
pub enum LocalOrGlobal {
Local(DeclID),
Global(Span)
}


#[derive(Debug, Clone)]
#[derive(Debug)]
pub enum TypeExpression {
Named, // SpanTypeExpression Span gives name
Array(Box<(SpanTypeExpression, SpanExpression)>)
}

pub type SpanTypeExpression = (TypeExpression, Span);

#[derive(Debug,Clone)]
#[derive(Debug)]
pub struct SignalDeclaration {
pub span : Span,
pub name_token : usize,
pub typ : SpanTypeExpression,
pub name : Box<str>,
Expand All @@ -135,9 +128,14 @@ impl Display for Operator {
}
}

#[derive(Debug,Clone)]
#[derive(Debug)]
pub struct Identifier {
pub span : Span
}

#[derive(Debug)]
pub enum Expression {
Named(LocalOrGlobal),
Named(Identifier),
Constant(Value),
UnaryOp(Box<(Operator, usize/*Operator token */, SpanExpression)>),
BinOp(Box<(SpanExpression, Operator, usize/*Operator token */, SpanExpression)>),
Expand All @@ -152,13 +150,12 @@ impl Expression {
}
}
pub type SpanExpression = (Expression, Span);
pub type SpanAssignableExpression = (AssignableExpression, Span);
pub type SpanStatement = (Statement, Span);

#[derive(Debug)]
pub enum AssignableExpression {
Named{local_idx : DeclID},
ArrayIndex(Box<(SpanAssignableExpression, SpanExpression, Span/* Brackets */)>)
pub enum LeftExpression {
Assignable(Expression),
Declaration(SignalDeclaration)
}

#[derive(Debug)]
Expand All @@ -170,7 +167,8 @@ pub enum AssignableExpressionModifiers {

#[derive(Debug)]
pub struct AssignableExpressionWithModifiers {
pub expr : SpanAssignableExpression,
pub expr : LeftExpression,
pub span : Span,
pub modifiers : AssignableExpressionModifiers
}

Expand All @@ -182,10 +180,9 @@ pub struct RangeExpression {

#[derive(Debug)]
pub enum Statement {
Declaration(DeclID),
Assign{to : Vec<AssignableExpressionWithModifiers>, eq_sign_position : Option<usize>, expr : SpanExpression}, // num_regs v = expr;
Assign{to : Vec<AssignableExpressionWithModifiers>, eq_sign_position : Option<usize>, expr : Option<SpanExpression>}, // num_regs v = expr;
If{condition : SpanExpression, then : CodeBlock, els : Option<CodeBlock>},
For{var : DeclID, range : RangeExpression, code : CodeBlock},
For{var : SignalDeclaration, range : RangeExpression, code : CodeBlock},
Block(CodeBlock)
}

Expand All @@ -209,6 +206,13 @@ impl LinkInfo {
}


#[derive(Debug)]
pub struct ParsedInterface {
pub ports : Vec<SignalDeclaration>,
pub outputs_start : usize
}


#[derive(Debug, Clone)]
pub struct InterfacePorts<ID : Clone + Copy> {
pub outputs_start : usize,
Expand Down Expand Up @@ -249,8 +253,7 @@ impl<ID : Clone + Copy> InterfacePorts<ID> {
pub struct Module {
pub link_info : LinkInfo,

pub declarations : FlatAlloc<SignalDeclaration, DeclIDMarker>,
pub ports : InterfacePorts<DeclID>,
pub interface : ParsedInterface,
pub code : CodeBlock,

pub flattened : FlattenedModule,
Expand All @@ -262,9 +265,9 @@ impl Module {
pub fn print_flattened_module(&self) {
println!("[[{}]]:", self.link_info.name);
println!("Interface:");
for ((port, is_input), port_decl) in zip(self.flattened.interface_ports.iter(), self.ports.ports.iter()) {
for (port, is_input) in self.flattened.interface_ports.iter() {
let port_direction = if is_input {"input"} else {"output"};
let port_name = &self.declarations[*port_decl].name;
let port_name = &self.flattened.instructions[port].extract_wire_declaration().name;
println!(" {port_direction} {port_name} -> {:?}", port);
}
println!("Instantiations:");
Expand Down
14 changes: 12 additions & 2 deletions src/dev_aid/lsp.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@

use std::{error::Error, ffi::OsStr, fs::read_dir, net::SocketAddr};
use std::{error::Error, ffi::OsStr, net::SocketAddr};
use lsp_types::{notification::*, request::Request, *};

use lsp_server::{Connection, Message, Response};

use lsp_types::notification::Notification;

use crate::{arena_alloc::ArenaVector, ast::{IdentifierType, Module, Span}, dev_aid::syntax_highlighting::create_token_ide_info, errors::{CompileError, ErrorCollector, ErrorLevel}, flattening::{FlatID, WireInstance, WireSource}, instantiation::{SubModuleOrWire, LATENCY_UNSET}, linker::{FileData, FileUUID, FileUUIDMarker, Linker, LocationInfo}, parser::perform_full_semantic_parse, tokenizer::{CharLine, TokenizeResult}, typing::Type};
use crate::{
arena_alloc::ArenaVector,
ast::{IdentifierType, Module, Span},
dev_aid::syntax_highlighting::create_token_ide_info,
errors::{CompileError, ErrorCollector, ErrorLevel},
flattening::FlatID,
instantiation::{SubModuleOrWire, LATENCY_UNSET},
linker::{FileData, FileUUID, FileUUIDMarker, Linker, LocationInfo},
parser::perform_full_semantic_parse,
tokenizer::{CharLine, TokenizeResult}
};

use super::syntax_highlighting::{IDETokenType, IDEIdentifierType, IDEToken};

Expand Down
Loading

0 comments on commit 8a8527d

Please sign in to comment.