Skip to content

Commit

Permalink
Also declare consts in core.sus At last, all have LinkInfo!
Browse files Browse the repository at this point in the history
  • Loading branch information
VonTum committed Nov 4, 2024
1 parent df46ba3 commit d391cb9
Show file tree
Hide file tree
Showing 20 changed files with 195 additions and 200 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ In this example, we create a memory block with a read port and a write port. Thi
- [x] Generative Code
- [x] Generative Parameters
- [x] Type Templates
- [ ] Full Template Inference
- [ ] Actions, Triggers and Queries

### Language Features
- [x] Basic Tokenizer
Expand Down Expand Up @@ -230,7 +232,7 @@ In this example, we create a memory block with a read port and a write port. Thi
- [x] Latency cuts
- [x] Latency Offset
- [ ] Latency Cuts & Latency Counting for "disjoint Input-Output blocks"
- [ ] Split Latencies
- [ ] ~~Split Latencies~~

### LSP
- [x] Basic LSP for VSCode integration
Expand Down
3 changes: 3 additions & 0 deletions src/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,9 @@ impl<T, IndexMarker> ArenaAllocator<T, IndexMarker> {
self.data.clear();
self.free_slots.clear();
}
pub fn is_empty(&self) -> bool {
self.data.len() == self.free_slots.len()
}
pub fn iter<'a>(&'a self) -> FlatOptionIterator<'a, T, IndexMarker> {
self.into_iter()
}
Expand Down
17 changes: 16 additions & 1 deletion src/compiler_top.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use std::path::PathBuf;
use std::str::FromStr;

use crate::config::EarlyExitUpTo;
use crate::linker::AFTER_INITIAL_PARSE_CP;
use crate::linker::{get_builtin_constant, get_builtin_type, AFTER_INITIAL_PARSE_CP};
use crate::prelude::*;
use crate::value::{TypedValue, Value};

use tree_sitter::Parser;

Expand Down Expand Up @@ -33,9 +34,20 @@ impl LinkerExtraFileInfoManager for () {}

impl Linker {
pub fn add_standard_library<ExtraInfoManager : LinkerExtraFileInfoManager>(&mut self, info_mngr : &mut ExtraInfoManager) {
assert!(self.modules.is_empty());
assert!(self.types.is_empty());
assert!(self.constants.is_empty());
println!("Standard Library Directory: {STD_LIB_PATH}");
let stl_path = PathBuf::from_str(STD_LIB_PATH).expect("Standard library directory is not a valid path?");
self.add_all_files_in_directory(&stl_path, info_mngr);

assert_eq!(self.types[get_builtin_type("int")].link_info.name, "int");
assert_eq!(self.types[get_builtin_type("bool")].link_info.name, "bool");

assert_eq!(self.constants[get_builtin_constant("true")].link_info.name, "true");
assert_eq!(self.constants[get_builtin_constant("false")].link_info.name, "false");
self.constants[get_builtin_constant("true")].val = TypedValue::from_value(Value::Bool(true));
self.constants[get_builtin_constant("false")].val = TypedValue::from_value(Value::Bool(false));
}

pub fn add_all_files_in_directory<ExtraInfoManager : LinkerExtraFileInfoManager>(&mut self, directory : &PathBuf, info_mngr : &mut ExtraInfoManager) {
Expand Down Expand Up @@ -128,6 +140,9 @@ impl Linker {
for (_, typ) in &mut self.types {
typ.link_info.reset_to(AFTER_INITIAL_PARSE_CP);
}
for (_, cst) in &mut self.constants {
cst.link_info.reset_to(AFTER_INITIAL_PARSE_CP);
}
if config().early_exit == EarlyExitUpTo::Initialize {return}

flatten_all_modules(self);
Expand Down
5 changes: 2 additions & 3 deletions src/dev_aid/lsp/hover_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,8 @@ pub fn hover(info: LocationInfo, linker: &Linker, file_data: &FileData) -> Vec<M
}
}
LocationInfo::Global(global) => {
if let Some(link_info) = linker.get_link_info(global) {
hover.documentation_link_info(link_info);
}
let link_info = linker.get_link_info(global);
hover.documentation_link_info(link_info);
hover.sus_code(format!("{}", linker.get_full_name(global)));
match global {
NameElem::Module(md_uuid) => {
Expand Down
10 changes: 4 additions & 6 deletions src/dev_aid/lsp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ fn initialize_all_files(init_params: &InitializeParams) -> (Linker, LSPFileManag
fn gather_completions(linker: &Linker, file_id: FileUUID, position: usize) -> Vec<CompletionItem> {
let mut result = Vec::new();

use crate::linker::Linkable;
for (_, m) in &linker.modules {
result.push(CompletionItem {
label: m.link_info.name.to_string(),
Expand All @@ -241,14 +240,14 @@ fn gather_completions(linker: &Linker, file_id: FileUUID, position: usize) -> Ve
}
for (_, c) in &linker.constants {
result.push(CompletionItem {
label: c.get_name().to_string(),
label: c.link_info.name.to_string(),
kind: Some(CompletionItemKind::CONSTANT),
..Default::default()
});
}
for (_, t) in &linker.types {
result.push(CompletionItem {
label: t.get_name().to_string(),
label: t.link_info.name.to_string(),
kind: Some(CompletionItemKind::STRUCT),
..Default::default()
});
Expand Down Expand Up @@ -397,9 +396,8 @@ fn handle_request(
goto_definition_list.push((template_arg.name_span, link_info.file))
}
LocationInfo::Global(id) => {
if let Some(link_info) = linker.get_link_info(id) {
goto_definition_list.push((link_info.name_span, link_info.file));
}
let link_info = linker.get_link_info(id);
goto_definition_list.push((link_info.name_span, link_info.file));
}
LocationInfo::Port(_sm, md, port_id) => {
goto_definition_list.push((md.ports[port_id].name_span, md.link_info.file));
Expand Down
15 changes: 12 additions & 3 deletions src/dev_aid/lsp/tree_walk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ impl<'linker, Visitor: FnMut(Span, LocationInfo<'linker>), Pruner: Fn(Span) -> b
let target_name_elem = NameElem::from(global.id);
self.visit(global.total_span, LocationInfo::Global(target_name_elem));
for (id, template_arg) in global.template_args.iter_valids() {
let target_link_info = self.linker.get_link_info(target_name_elem).unwrap();
let target_link_info = self.linker.get_link_info(target_name_elem);
self.visit(
template_arg.name_span,
LocationInfo::TemplateInput(
Expand Down Expand Up @@ -442,6 +442,15 @@ impl<'linker, Visitor: FnMut(Span, LocationInfo<'linker>), Pruner: Fn(Span) -> b
}
}

fn walk_constant(&mut self, cst_id: ConstantUUID) {
let cst = &self.linker.constants[cst_id];
if !(self.should_prune)(cst.link_info.span) {
self.walk_name_and_template_arguments(NameElem::Constant(cst_id), &cst.link_info);

println!("TODO constant instructions")
}
}

fn walk_file(&mut self, file: &'linker FileData) {
for global in &file.associated_values {
match *global {
Expand All @@ -451,8 +460,8 @@ impl<'linker, Visitor: FnMut(Span, LocationInfo<'linker>), Pruner: Fn(Span) -> b
NameElem::Type(typ_id) => {
self.walk_struct(typ_id);
}
NameElem::Constant(_) => {
todo!()
NameElem::Constant(cst_id) => {
self.walk_constant(cst_id);
}
}
}
Expand Down
62 changes: 50 additions & 12 deletions src/flattening/flatten.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,16 @@ impl core::fmt::Display for BinaryOperator {
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum GenerativeKind {
PlainGenerative,
ForLoopGenerative
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum DeclarationContext {
IO{is_input : bool},
ForLoopGenerative,
Generative(GenerativeKind),
TemplateGenerative(TemplateID),
PlainWire,
StructField
Expand Down Expand Up @@ -235,7 +241,7 @@ struct FlatteningContext<'l, 'errs> {
default_declaration_context: DeclarationContext
}

impl<'l, 'errs : 'l> FlatteningContext<'l, 'errs> {
impl<'l, 'errs> FlatteningContext<'l, 'errs> {
fn flatten_template_inputs(&mut self, cursor: &mut Cursor) {
let mut template_inputs_to_visit = self.working_on_link_info.template_arguments.id_range().into_iter();
if cursor.optional_field(field!("template_declaration_arguments")) {
Expand Down Expand Up @@ -583,9 +589,9 @@ impl<'l, 'errs : 'l> FlatteningContext<'l, 'errs> {
}
DeclarationPortInfo::RegularPort { is_input, port_id: PortID::PLACEHOLDER }
}
DeclarationContext::ForLoopGenerative => {
DeclarationContext::Generative(_) => {
if let Some((_, io_span)) = io_kw {
self.errors.error(io_span, "Cannot declare 'input' or 'output' to the iterator of a for loop");
self.errors.error(io_span, "Cannot declare 'input' or 'output' to declarations in a generative context");
}
DeclarationPortInfo::NotPort
}
Expand Down Expand Up @@ -635,7 +641,7 @@ impl<'l, 'errs : 'l> FlatteningContext<'l, 'errs> {
}
}
}
DeclarationContext::ForLoopGenerative | DeclarationContext::TemplateGenerative(_) => {
DeclarationContext::Generative(_) | DeclarationContext::TemplateGenerative(_) => {
if let Some((_, modifier_span)) = declaration_modifiers {
self.errors.error(modifier_span, "Cannot add modifiers to implicitly generative declarations");
}
Expand Down Expand Up @@ -1282,7 +1288,7 @@ impl<'l, 'errs : 'l> FlatteningContext<'l, 'errs> {
cursor.go_down_no_check(|cursor| {
let loop_var_decl_frame = self.local_variable_context.new_frame();
cursor.field(field!("for_decl"));
let loop_var_decl = self.flatten_declaration::<false>(DeclarationContext::ForLoopGenerative, true, true, cursor);
let loop_var_decl = self.flatten_declaration::<false>(DeclarationContext::Generative(GenerativeKind::ForLoopGenerative), true, true, cursor);

cursor.field(field!("from"));
let (start, start_is_generative) = self.flatten_expr(cursor);
Expand Down Expand Up @@ -1467,14 +1473,38 @@ impl<'l, 'errs : 'l> FlatteningContext<'l, 'errs> {
let _ = cursor.optional_field(field!("extern_marker"));
// Skip because we know this from initialization.
cursor.field(field!("object_type"));

// We parse this one a bit strangely. Just because visually it looks nicer to have the template arguments after
// const int[SIZE] range #(int SIZE) {}
let const_type_cursor = (cursor.kind() == kind!("const_and_type")).then(|| cursor.clone());

let name_span = cursor.field_span(field!("name"), kind!("identifier"));
self.flatten_template_inputs(cursor);
let module_name = &self.globals.file_data.file_text[name_span];
println!("TREE SITTER module! {module_name}");
// Interface is allocated in self
if cursor.optional_field(field!("interface_ports")) {
self.flatten_interface_ports(cursor);

if let Some(mut const_type_cursor) = const_type_cursor {
let decl_span = const_type_cursor.span();
const_type_cursor.go_down(kind!("const_and_type"), |const_type_cursor| {
const_type_cursor.field(field!("const_type"));
let typ_expr = self.flatten_type(const_type_cursor);
let module_output_decl = self.instructions.alloc(Instruction::Declaration(Declaration{
typ_expr,
typ: self.type_alloc.alloc_unset_type(DomainAllocOption::Generative),
decl_span,
name_span,
name: module_name.to_string(),
declaration_runtime_depth: OnceCell::new(),
read_only: false,
declaration_itself_is_not_written_to: true,
is_port: DeclarationPortInfo::NotPort,
identifier_type: IdentifierType::Generative,
latency_specifier: None,
documentation: const_type_cursor.extract_gathered_comments(),
}));

self.alloc_local_name(name_span, NamedLocal::Declaration(module_output_decl));
});
}

cursor.field(field!("block"));
Expand Down Expand Up @@ -1511,7 +1541,7 @@ pub fn flatten_all_modules(linker: &mut Linker) {
(UUIDRange::empty().into_iter(), typ.fields.id_range().into_iter(), DeclarationContext::StructField)
}
NameElem::Constant(const_uuid) => {
todo!("TODO Constant flattening")
(UUIDRange::empty().into_iter(), UUIDRange::empty().into_iter(), DeclarationContext::Generative(GenerativeKind::PlainGenerative))
}
};

Expand All @@ -1521,7 +1551,7 @@ pub fn flatten_all_modules(linker: &mut Linker) {
fields_to_visit,
default_declaration_context,
errors: &globals.errors,
working_on_link_info: linker.get_link_info(global_obj).unwrap(),
working_on_link_info: linker.get_link_info(global_obj),
instructions: FlatAlloc::new(),
type_alloc: TypingAllocator { type_variable_alloc: UUIDAllocator::new(), domain_variable_alloc: UUIDAllocator::new() },
named_domain_alloc: UUIDAllocator::new(),
Expand Down Expand Up @@ -1600,7 +1630,15 @@ pub fn flatten_all_modules(linker: &mut Linker) {
&mut typ.link_info
}
NameElem::Constant(const_uuid) => {
todo!("TODO Constant flattening")
let cst = &mut linker.constants[const_uuid];

cst.output_decl = instructions.iter().find(|(_decl_id, instr)| {
if let Instruction::Declaration(decl) = instr {
decl.name_span == cst.link_info.name_span
} else {false}
}).unwrap().0;

&mut cst.link_info
}
};

Expand Down
29 changes: 17 additions & 12 deletions src/flattening/initialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use arrayvec::ArrayVec;
use sus_proc_macro::{field, kind, kw};

use crate::errors::ErrorStore;
use crate::linker::{IsExtern, AFTER_INITIAL_PARSE_CP};
use crate::linker::{IsExtern, NamedConstant, AFTER_INITIAL_PARSE_CP};
use crate::prelude::*;

use crate::linker::{FileBuilder, LinkInfo, ResolvedGlobals};
use crate::value::TypedValue;
use crate::{file_position::FileText, flattening::Module, instantiation::InstantiationList};

use crate::typing::template::{
Expand Down Expand Up @@ -278,8 +279,8 @@ fn initialize_global_object(builder: &mut FileBuilder, parsing_errors: ErrorColl
kw!("module") => {
GlobalObjectKind::Module
}
kw!("function") => {
GlobalObjectKind::Function
kind!("const_and_type") => {
GlobalObjectKind::Const
}
kw!("struct") => {
GlobalObjectKind::Struct
Expand Down Expand Up @@ -315,27 +316,31 @@ fn initialize_global_object(builder: &mut FileBuilder, parsing_errors: ErrorColl
link_info.reabsorb_errors_globals((parsing_errors, ResolvedGlobals::empty()), AFTER_INITIAL_PARSE_CP);

match global_obj_kind {
GlobalObjectKind::Module | GlobalObjectKind::Function => {
let md = Module {
GlobalObjectKind::Module => {
builder.add_module(Module {
link_info,
instructions: FlatAlloc::new(),
ports: ctx.ports,
domain_names: ctx.domains,
domains: FlatAlloc::new(),
interfaces: ctx.interfaces,
instantiations: InstantiationList::new(),
};

builder.add_module(md);
});
}
GlobalObjectKind::Struct => {
let typ = StructType {
builder.add_type(StructType {
link_info,
fields: ctx.fields,
instructions: FlatAlloc::new()
};

builder.add_type(typ);
});
}
GlobalObjectKind::Const => {
builder.add_const(NamedConstant {
link_info,
instructions: FlatAlloc::new(),
output_decl: FlatID::PLACEHOLDER,
val: TypedValue::make_placeholder(),
});
}
}
}
2 changes: 1 addition & 1 deletion src/flattening/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use crate::typing::{
#[derive(Debug)]
pub enum GlobalObjectKind {
Module,
Function,
Const,
Struct
}

Expand Down
1 change: 1 addition & 0 deletions src/flattening/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ fn print_current_node_indented<'ft>(file_text: &'ft FileText, cursor: &TreeCurso
node_name
}

#[derive(Clone)]
pub struct Cursor<'t> {
cursor: TreeCursor<'t>,
file_text: &'t FileText,
Expand Down
Loading

0 comments on commit d391cb9

Please sign in to comment.