mirror of
https://github.com/Dummi26/mers.git
synced 2025-12-14 11:16:17 +01:00
Type Annotations
- Add type annotations: [type] statement - Add type definitions: [[name] type], [[name] := statement] - Add type annotations example (08) - add Quickstart.md, reference it from README
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
use std::sync::{Arc, RwLock};
|
||||
|
||||
use crate::{
|
||||
data::{Data, Type},
|
||||
data::{self, Data, MersType},
|
||||
errors::CheckError,
|
||||
info::Local,
|
||||
program::run::CheckInfo,
|
||||
};
|
||||
|
||||
mod with_base;
|
||||
@@ -58,11 +60,27 @@ impl Config {
|
||||
}
|
||||
|
||||
pub fn new() -> Self {
|
||||
let mut info_check: CheckInfo = Default::default();
|
||||
macro_rules! init_d {
|
||||
($e:expr) => {
|
||||
let t = $e;
|
||||
info_check
|
||||
.scopes
|
||||
.last_mut()
|
||||
.unwrap()
|
||||
.types
|
||||
.insert(t.to_string(), Ok(Arc::new(t)));
|
||||
};
|
||||
}
|
||||
init_d!(data::bool::BoolT);
|
||||
init_d!(data::int::IntT);
|
||||
init_d!(data::float::FloatT);
|
||||
init_d!(data::string::StringT);
|
||||
Self {
|
||||
globals: 0,
|
||||
info_parsed: Default::default(),
|
||||
info_run: Default::default(),
|
||||
info_check: Default::default(),
|
||||
info_check,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,8 +92,15 @@ impl Config {
|
||||
self.globals += 1;
|
||||
self
|
||||
}
|
||||
pub fn add_type(self, _name: String, _t: Type) -> Self {
|
||||
// TODO! needed for type syntax in the parser, everything else probably(?) works already
|
||||
pub fn add_type(
|
||||
mut self,
|
||||
name: String,
|
||||
t: Result<
|
||||
Arc<dyn MersType>,
|
||||
Arc<dyn Fn(&str, &CheckInfo) -> Result<Arc<dyn MersType>, CheckError> + Send + Sync>,
|
||||
>,
|
||||
) -> Self {
|
||||
self.info_check.scopes[0].types.insert(name, t);
|
||||
self
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ use std::{
|
||||
|
||||
use crate::{
|
||||
data::{self, Data, MersData, MersType, Type},
|
||||
parsing::{statements::to_string_literal, Source},
|
||||
program::{self, run::CheckInfo},
|
||||
};
|
||||
|
||||
@@ -20,7 +21,10 @@ impl Config {
|
||||
/// `get_mut: fn` like get, but returns a reference to the object
|
||||
pub fn with_list(self) -> Self {
|
||||
// TODO: Type with generics
|
||||
self.add_type("List".to_string(), Type::new(ListT(Type::empty_tuple())))
|
||||
self.add_type("List".to_string(),
|
||||
Err(Arc::new(|s, i| {
|
||||
let t = crate::parsing::types::parse_type(&mut Source::new_from_string_raw(s.to_owned()))?;
|
||||
Ok(Arc::new(ListT(crate::parsing::types::type_from_parsed(&t, i)?)))})))
|
||||
.add_var(
|
||||
"pop".to_string(),
|
||||
Data::new(data::function::Function {
|
||||
@@ -202,7 +206,10 @@ impl MersType for ListT {
|
||||
.is_some_and(|v| self.0.is_same_type_as(&v.0))
|
||||
}
|
||||
fn is_included_in_single(&self, target: &dyn MersType) -> bool {
|
||||
self.is_same_type_as(target)
|
||||
target
|
||||
.as_any()
|
||||
.downcast_ref::<Self>()
|
||||
.is_some_and(|v| self.0.is_included_in(&v.0))
|
||||
}
|
||||
fn subtypes(&self, acc: &mut Type) {
|
||||
for t in self.0.subtypes_type().types {
|
||||
@@ -234,7 +241,7 @@ impl Display for List {
|
||||
}
|
||||
impl Display for ListT {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "[{}]", self.0)?;
|
||||
write!(f, "List<{}>", to_string_literal(&self.0.to_string(), '>'))?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ use std::{
|
||||
use crate::{
|
||||
data::{self, Data, MersData, MersType, Type},
|
||||
errors::CheckError,
|
||||
parsing::{statements::to_string_literal, Source},
|
||||
program::{self, run::CheckInfo},
|
||||
};
|
||||
|
||||
@@ -19,7 +20,10 @@ impl Config {
|
||||
pub fn with_multithreading(self) -> Self {
|
||||
self.add_type(
|
||||
"Thread".to_string(),
|
||||
Type::new(ThreadT(Type::empty_tuple())),
|
||||
Err(Arc::new(|s, i| {
|
||||
let t = crate::parsing::types::parse_type(&mut Source::new_from_string_raw(s.to_owned()))?;
|
||||
Ok(Arc::new(ThreadT(crate::parsing::types::type_from_parsed(&t, i)?)))
|
||||
})),
|
||||
)
|
||||
.add_var(
|
||||
"thread".to_string(),
|
||||
@@ -170,6 +174,6 @@ impl Display for Thread {
|
||||
}
|
||||
impl Display for ThreadT {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "<Thread>")
|
||||
write!(f, "Thread<{}>", to_string_literal(&self.0.to_string(), '>'))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user