Skip to content

Commit

Permalink
Delete ConcreteType::Error & evaluate constants in execute.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
VonTum committed Nov 7, 2024
1 parent a3a52b1 commit ac35308
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 47 deletions.
2 changes: 1 addition & 1 deletion src/codegen_fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ fn typ_to_declaration(mut typ: &ConcreteType, var_name: &str) -> String {
}
}
ConcreteType::Array(_) => unreachable!("All arrays have been used up already"),
ConcreteType::Value(_) | ConcreteType::Unknown | ConcreteType::Error => unreachable!(),
ConcreteType::Value(_) | ConcreteType::Unknown => unreachable!(),
}
}

Expand Down
8 changes: 1 addition & 7 deletions src/compiler_top.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ use std::path::PathBuf;
use std::str::FromStr;

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

use tree_sitter::Parser;

Expand Down Expand Up @@ -43,11 +42,6 @@ impl Linker {

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
4 changes: 1 addition & 3 deletions src/flattening/initialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ 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 @@ -336,8 +335,7 @@ fn initialize_global_object(builder: &mut FileBuilder, parsing_errors: ErrorColl
GlobalObjectKind::Const => {
builder.add_const(NamedConstant {
link_info,
output_decl: FlatID::PLACEHOLDER,
val: TypedValue::make_placeholder(),
output_decl: FlatID::PLACEHOLDER
});
}
}
Expand Down
21 changes: 18 additions & 3 deletions src/instantiation/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

use std::ops::{Deref, Index, IndexMut};

use crate::linker::{get_builtin_type, IsExtern};
use crate::prelude::*;
use crate::typing::template::GlobalReference;

use num::BigInt;

Expand Down Expand Up @@ -191,6 +193,20 @@ impl<'fl, 'l> InstantiationContext<'fl, 'l> {
}
}

fn get_named_constant_value(&self, cst_ref: &GlobalReference<ConstantUUID>) -> TypedValue {
let linker_cst = &self.linker.constants[cst_ref.id];

if linker_cst.link_info.is_extern == IsExtern::Builtin {
match linker_cst.link_info.name.as_str() {
"true" => TypedValue{value: Value::Bool(true), typ: ConcreteType::Named(get_builtin_type("bool"))},
"false" => TypedValue{value: Value::Bool(false), typ: ConcreteType::Named(get_builtin_type("bool"))},
other => unreachable!("{other} is not a known builtin constant")
}
} else {
todo!("Custom Constants");
}
}

// Points to the wire in the hardware that corresponds to the root of this.
fn determine_wire_ref_root(&mut self, wire_ref_root: &WireReferenceRoot) -> RealWireRefRoot {
match wire_ref_root {
Expand All @@ -204,8 +220,7 @@ impl<'fl, 'l> InstantiationContext<'fl, 'l> {
SubModuleOrWire::Unnasigned => unreachable!(),
},
WireReferenceRoot::NamedConstant(cst) => {
let cst = &self.linker.constants[cst.id];
RealWireRefRoot::Constant(cst.val.clone())
RealWireRefRoot::Constant(self.get_named_constant_value(cst))
}
WireReferenceRoot::SubModulePort(port) => {
return self.instantiate_port_wire_ref_root(
Expand Down Expand Up @@ -358,7 +373,7 @@ impl<'fl, 'l> InstantiationContext<'fl, 'l> {
self.generation_state.get_generation_value(decl_id)?.clone()
}
WireReferenceRoot::NamedConstant(cst) => {
self.linker.constants[cst.id].get_value().clone()
self.get_named_constant_value(cst)
}
&WireReferenceRoot::SubModulePort(_) => {
todo!("Don't yet support compile time functions")
Expand Down
25 changes: 3 additions & 22 deletions src/linker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ use crate::{
alloc::ArenaAllocator,
file_position::FileText,
flattening::Module,
util::{const_str_position, const_str_position_in_tuples},
value::{TypedValue, Value},
util::const_str_position,
};

use crate::errors::{CompileError, ErrorInfo, ErrorLevel, ErrorStore};
Expand All @@ -30,9 +29,6 @@ use self::checkpoint::CheckPoint;

const BUILTIN_TYPES: [&'static str; 2] = ["bool", "int"];

const BUILTIN_CONSTANTS: [(&'static str, Value); 2] =
[("true", Value::Bool(true)), ("false", Value::Bool(false))];

pub const fn get_builtin_type(name: &'static str) -> TypeUUID {
if let Some(is_type) = const_str_position(name, &BUILTIN_TYPES) {
TypeUUID::from_hidden_value(is_type)
Expand All @@ -41,14 +37,6 @@ pub const fn get_builtin_type(name: &'static str) -> TypeUUID {
}
}

pub const fn get_builtin_constant(name: &'static str) -> ConstantUUID {
if let Some(is_constant) = const_str_position_in_tuples(name, &BUILTIN_CONSTANTS) {
ConstantUUID::from_hidden_value(is_constant)
} else {
unreachable!()
}
}

#[derive(Debug, Clone)]
pub struct Documentation {
pub gathered: Box<[Span]>,
Expand All @@ -69,7 +57,7 @@ impl Documentation {
}
}

#[derive(Debug)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum IsExtern {
Normal,
Extern,
Expand Down Expand Up @@ -149,14 +137,7 @@ pub struct LinkingErrorLocation {
#[derive(Debug)]
pub struct NamedConstant {
pub link_info: LinkInfo,
pub output_decl: FlatID,
pub val: TypedValue
}

impl NamedConstant {
pub fn get_value(&self) -> &TypedValue {
&self.val
}
pub output_decl: FlatID
}

pub struct FileData {
Expand Down
1 change: 0 additions & 1 deletion src/to_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ impl ConcreteType {
}
ConcreteType::Value(v) => format!("{{concrete_type_{v}}}"),
ConcreteType::Unknown => format!("{{concrete_type_unknown}}"),
ConcreteType::Error => format!("{{concrete_type_error}}"),
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/typing/concrete_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ pub enum ConcreteType {
Named(TypeUUID),
Value(Value),
Array(Box<(ConcreteType, ConcreteType)>),
Unknown,
Error,
Unknown
}

/// Panics on Type Errors that should have been caught by [AbstractType]
Expand Down Expand Up @@ -104,7 +103,6 @@ impl ConcreteType {
&& target_arr_size.type_compare(found_arr_size)
}
(ConcreteType::Value(lv), ConcreteType::Value(rv)) => lv == rv,
(ConcreteType::Error, _) | (_, ConcreteType::Error) => true, // Just assume correct, because the other side has an error
(ConcreteType::Unknown, _) | (_, ConcreteType::Unknown) => {
todo!("Type Unification {self:?} {found:?}")
}
Expand Down
8 changes: 1 addition & 7 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,6 @@ impl TypedValue {
value: Value::Integer(i),
}
}
pub fn make_placeholder() -> Self {
Self {
typ: ConcreteType::Error,
value: Value::Error
}
}
/// panics if the value can't be typed.
pub fn from_value(value: Value) -> Self {
Self {
Expand Down Expand Up @@ -217,7 +211,7 @@ impl ConcreteType {
}
Value::Array(arr.into_boxed_slice())
}
ConcreteType::Value(_) | ConcreteType::Unknown | ConcreteType::Error => unreachable!(),
ConcreteType::Value(_) | ConcreteType::Unknown => unreachable!(),
}
}

Expand Down

0 comments on commit ac35308

Please sign in to comment.