Skip to content

Commit 4d6f120

Browse files
committed
Type function works as a successful gate
1 parent ef80c53 commit 4d6f120

File tree

5 files changed

+86
-33
lines changed

5 files changed

+86
-33
lines changed

crates/lust-lib/src/grammar/tokens/equals_op.rs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use crate::grammar::LuaExpression;
1+
use crate::{
2+
grammar::LuaExpression,
3+
typecheck::{LustType, TypeGate},
4+
};
25

36
#[derive(PartialEq, PartialOrd, Debug, Clone)]
47
pub struct EqualsOperation {
@@ -13,6 +16,39 @@ impl EqualsOperation {
1316
right: Box::new(right),
1417
}
1518
}
19+
20+
pub fn get_type_gate(&self) -> Option<TypeGate> {
21+
// panic!("Hellxo? {:?}", self);
22+
// type(varname) == "string"
23+
match (&*self.left, &*self.right) {
24+
(LuaExpression::FunctionCall(call), LuaExpression::StringLiteral(value))
25+
| (LuaExpression::StringLiteral(value), LuaExpression::FunctionCall(call)) => {
26+
let var_name = if call.function_name == "type" && call.arguments.len() == 1 {
27+
if let LuaExpression::VarName(name) = &call.arguments[0] {
28+
name
29+
} else {
30+
return None;
31+
}
32+
} else {
33+
return None;
34+
};
35+
36+
let lust_type = runtime_string_to_type(value)?;
37+
Some(TypeGate::new_single(var_name.clone(), [lust_type.clone()]))
38+
}
39+
_ => None,
40+
}
41+
}
42+
}
43+
44+
fn runtime_string_to_type(value: &str) -> Option<LustType> {
45+
match value {
46+
"nil" => Some(LustType::Nil),
47+
"boolean" => Some(LustType::boolean()),
48+
"number" => Some(LustType::Number),
49+
"string" => Some(LustType::String),
50+
_ => None,
51+
}
1652
}
1753

1854
impl std::fmt::Display for EqualsOperation {

crates/lust-lib/src/grammar/tokens/expression.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ impl LuaExpression {
3939
match self {
4040
LuaExpression::VarName(name) => Some(TypeGate::new_truthy(name.clone(), true)),
4141
LuaExpression::AndOperation(and_op) => and_op.get_type_gate(),
42+
LuaExpression::EqualsOperation(eq_op) => eq_op.get_type_gate(),
4243
_ => None,
4344
}
4445
}

crates/lust-lib/src/typecheck/gates/mod.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use crate::typecheck::LustType;
22

33
mod and;
4-
mod truthy;
4+
mod single;
55

66
pub use and::*;
7-
pub use truthy::*;
7+
pub use single::*;
88

99
#[derive(Debug, Clone, PartialEq, PartialOrd)]
1010
pub enum TypeGate {
11-
Truthy(TruthyGate),
11+
Single(SingleGate),
1212
And(AndGate),
1313
}
1414

@@ -34,7 +34,16 @@ impl TypeGate {
3434
}
3535

3636
pub fn new_truthy(varname: String, truthy: bool) -> Self {
37-
Self::Truthy(TruthyGate::new(varname, truthy))
37+
let falsy_types = [LustType::Nil, LustType::False];
38+
if truthy {
39+
Self::Single(SingleGate::new_exclude(varname, falsy_types))
40+
} else {
41+
Self::Single(SingleGate::new_intersect(varname, falsy_types))
42+
}
43+
}
44+
45+
pub fn new_single(varname: String, types: impl IntoIterator<Item = LustType>) -> Self {
46+
Self::Single(SingleGate::new_intersect(varname, types))
3847
}
3948

4049
pub fn new_and(gates: impl IntoIterator<Item = TypeGate>) -> Self {
@@ -43,7 +52,7 @@ impl TypeGate {
4352

4453
pub fn get_restrictions(&self) -> &[GateRestriction] {
4554
match self {
46-
TypeGate::Truthy(gate) => gate.get_restrictions(),
55+
TypeGate::Single(gate) => gate.get_restrictions(),
4756
TypeGate::And(gate) => gate.get_restrictions(),
4857
}
4958
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use crate::typecheck::{GateRestriction, GateRestrictionKind, LustType};
2+
3+
#[derive(Debug, Clone, PartialEq, PartialOrd)]
4+
pub struct SingleGate {
5+
pub restriction: [GateRestriction; 1],
6+
}
7+
8+
impl SingleGate {
9+
pub fn new_intersect(varname: String, types: impl IntoIterator<Item = LustType>) -> Self {
10+
let restriction = GateRestriction {
11+
varname,
12+
kind: GateRestrictionKind::Intersect,
13+
types: types.into_iter().collect(),
14+
};
15+
Self {
16+
restriction: [restriction],
17+
}
18+
}
19+
20+
pub fn new_exclude(varname: String, types: impl IntoIterator<Item = LustType>) -> Self {
21+
let restriction = GateRestriction {
22+
varname,
23+
kind: GateRestrictionKind::Exclude,
24+
types: types.into_iter().collect(),
25+
};
26+
Self {
27+
restriction: [restriction],
28+
}
29+
}
30+
31+
pub fn get_restrictions(&self) -> &[GateRestriction] {
32+
&self.restriction
33+
}
34+
}

crates/lust-lib/src/typecheck/gates/truthy.rs

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)