Super basic work on binder, rework of parser to include their spans

This commit is contained in:
Deukhoofd 2022-04-08 16:55:45 +02:00
parent 32636c7724
commit 4e096069ad
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
22 changed files with 2504 additions and 1206 deletions

5
src/binding/mod.rs Normal file
View File

@ -0,0 +1,5 @@
use crate::parsing::parser::parsed_statement::ParsedStatement;
mod type_registry;
pub fn bind(_statement: Box<ParsedStatement>) {}

View File

@ -0,0 +1,38 @@
use crate::parsing::parser::parsed_statement::{ParsedStatement, ParsedStatementData};
use crate::types::script_type::ScriptType;
use crate::types::script_type_namespace::ScriptTypeNamespace;
use crate::types::type_library::TypeLibrary;
pub fn register_types(statement: &ParsedStatement) -> TypeLibrary {
let mut library = TypeLibrary::new();
library = find_types_in_statement(statement, library, None);
library
}
fn find_types_in_statement(
statement: &ParsedStatement,
mut library: TypeLibrary,
active_namespace: Option<ScriptTypeNamespace>,
) -> TypeLibrary {
match &statement.data {
ParsedStatementData::Script { statements } => {
for statement in statements {
library = find_types_in_statement(statement.as_ref(), library, None);
}
}
ParsedStatementData::ClassDeclaration { name, .. } => {
let script_type = ScriptType::new();
if let Some(mut ns) = active_namespace {
ns.register(name, script_type);
} else {
library
.get_global_namespace_mut()
.register(name, script_type);
};
}
_ => {}
}
library
}

View File

@ -257,9 +257,11 @@ fn integration_add_function() {
println!("Parsed Tree JSON: {}", serde_json::to_string(&parsed_tree).unwrap()); println!("Parsed Tree JSON: {}", serde_json::to_string(&parsed_tree).unwrap());
let expected_tree: Box<ParsedStatement> = let expected_tree: Box<ParsedStatement> =
serde_json::from_str(r#"{ serde_json::from_str(r#"{
"data": {
"Script": { "Script": {
"statements": [ "statements": [
{ {
"data": {
"FuncDeclaration": { "FuncDeclaration": {
"modifiers": 0, "modifiers": 0,
"field_mod": null, "field_mod": null,
@ -267,51 +269,88 @@ fn integration_add_function() {
"is_constructor": false, "is_constructor": false,
"returns_reference": false, "returns_reference": false,
"return_type": { "return_type": {
"data": {
"Type": { "Type": {
"is_const": false, "is_const": false,
"scope": null, "scope": null,
"datatype": { "datatype": {
"data": {
"DataTypePrimType": { "DataTypePrimType": {
"prim_type": "Int32" "prim_type": "Int32"
} }
}, },
"span": {
"start": 0,
"end": 3
}
},
"modifiers": [] "modifiers": []
} }
}, },
"span": {
"start": 0,
"end": 3
}
},
"name": "add", "name": "add",
"param_list": { "param_list": {
"data": {
"ParamList": { "ParamList": {
"parameters": [ "parameters": [
{ {
"parameter_type": { "parameter_type": {
"data": {
"Type": { "Type": {
"is_const": false, "is_const": false,
"scope": null, "scope": null,
"datatype": { "datatype": {
"data": {
"DataTypePrimType": { "DataTypePrimType": {
"prim_type": "Int32" "prim_type": "Int32"
} }
}, },
"span": {
"start": 8,
"end": 11
}
},
"modifiers": [] "modifiers": []
} }
}, },
"span": {
"start": 8,
"end": 11
}
},
"type_mod": null, "type_mod": null,
"identifier": "a", "identifier": "a",
"default": null "default": null
}, },
{ {
"parameter_type": { "parameter_type": {
"data": {
"Type": { "Type": {
"is_const": false, "is_const": false,
"scope": null, "scope": null,
"datatype": { "datatype": {
"data": {
"DataTypePrimType": { "DataTypePrimType": {
"prim_type": "Int32" "prim_type": "Int32"
} }
}, },
"span": {
"start": 15,
"end": 18
}
},
"modifiers": [] "modifiers": []
} }
}, },
"span": {
"start": 15,
"end": 18
}
},
"type_mod": null, "type_mod": null,
"identifier": "b", "identifier": "b",
"default": null "default": null
@ -319,40 +358,86 @@ fn integration_add_function() {
] ]
} }
}, },
"span": {
"start": 7,
"end": 21
}
},
"is_const": false, "is_const": false,
"func_attr": 0, "func_attr": 0,
"block": { "block": {
"data": {
"StatBlock": { "StatBlock": {
"statements": [ "statements": [
{ {
"data": {
"ReturnStatement": { "ReturnStatement": {
"expression": { "expression": {
"data": {
"BinaryExpr": { "BinaryExpr": {
"left": { "left": {
"data": {
"VarAccess": { "VarAccess": {
"scope": null, "scope": null,
"identifier": "a" "identifier": "a"
} }
}, },
"span": {
"start": 35,
"end": 36
}
},
"operator": "Addition", "operator": "Addition",
"right": { "right": {
"data": {
"VarAccess": { "VarAccess": {
"scope": null, "scope": null,
"identifier": "b" "identifier": "b"
} }
},
"span": {
"start": 39,
"end": 40
} }
} }
} }
},
"span": {
"start": 35,
"end": 40
} }
} }
]
}
} }
},
"span": {
"start": 28,
"end": 41
} }
} }
] ]
} }
}"#).unwrap(); },
"span": {
"start": 22,
"end": 43
}
}
}
},
"span": {
"start": 0,
"end": 43
}
}
]
}
},
"span": {
"start": 0,
"end": 43
}
}
"#).unwrap();
assert_eq!(parsed_tree, expected_tree); assert_eq!(parsed_tree, expected_tree);
} }
// A substring of a script should never panic, even though it might be completely invalid. // A substring of a script should never panic, even though it might be completely invalid.

View File

@ -12,7 +12,8 @@ fn main() {
mod_file, mod_file,
r#"//////////////////////////// r#"////////////////////////////
// Automatically Generated// // Automatically Generated//
////////////////////////////"# ////////////////////////////
"#
) )
.unwrap(); .unwrap();

View File

@ -44,53 +44,92 @@ fn integration_class_with_many_statements() {
println!("Parsed Tree JSON: {}", serde_json::to_string(&parsed_tree).unwrap()); println!("Parsed Tree JSON: {}", serde_json::to_string(&parsed_tree).unwrap());
let expected_tree: Box<ParsedStatement> = let expected_tree: Box<ParsedStatement> =
serde_json::from_str(r#"{ serde_json::from_str(r#"{
"data": {
"Script": { "Script": {
"statements": [ "statements": [
{ {
"data": {
"ClassDeclaration": { "ClassDeclaration": {
"modifiers": 10, "modifiers": 10,
"name": "Foobar", "name": "Foobar",
"inherits": [], "inherits": [],
"statements": [ "statements": [
{ {
"data": {
"Var": { "Var": {
"modifier": 0, "modifier": 0,
"var_type": { "var_type": {
"data": {
"Type": { "Type": {
"is_const": false, "is_const": false,
"scope": null, "scope": null,
"datatype": { "datatype": {
"data": {
"DataTypePrimType": { "DataTypePrimType": {
"prim_type": "Int32" "prim_type": "Int32"
} }
}, },
"span": {
"start": 32,
"end": 35
}
},
"modifiers": [] "modifiers": []
} }
}, },
"span": {
"start": 32,
"end": 35
}
},
"identifier": "_a", "identifier": "_a",
"assignment": null "assignment": null
} }
}, },
"span": {
"start": 32,
"end": 39
}
},
{ {
"data": {
"Var": { "Var": {
"modifier": 0, "modifier": 0,
"var_type": { "var_type": {
"data": {
"Type": { "Type": {
"is_const": false, "is_const": false,
"scope": null, "scope": null,
"datatype": { "datatype": {
"data": {
"DataTypePrimType": { "DataTypePrimType": {
"prim_type": "Int32" "prim_type": "Int32"
} }
}, },
"span": {
"start": 44,
"end": 47
}
},
"modifiers": [] "modifiers": []
} }
}, },
"span": {
"start": 44,
"end": 47
}
},
"identifier": "_b", "identifier": "_b",
"assignment": null "assignment": null
} }
}, },
"span": {
"start": 44,
"end": 51
}
},
{ {
"data": {
"FuncDeclaration": { "FuncDeclaration": {
"modifiers": 0, "modifiers": 0,
"field_mod": null, "field_mod": null,
@ -98,51 +137,88 @@ fn integration_class_with_many_statements() {
"is_constructor": true, "is_constructor": true,
"returns_reference": false, "returns_reference": false,
"return_type": { "return_type": {
"data": {
"Type": { "Type": {
"is_const": false, "is_const": false,
"scope": null, "scope": null,
"datatype": { "datatype": {
"data": {
"DataTypeIdentifier": { "DataTypeIdentifier": {
"identifier": "Foobar" "identifier": "Foobar"
} }
}, },
"span": {
"start": 57,
"end": 63
}
},
"modifiers": [] "modifiers": []
} }
}, },
"span": {
"start": 57,
"end": 63
}
},
"name": "Foobar", "name": "Foobar",
"param_list": { "param_list": {
"data": {
"ParamList": { "ParamList": {
"parameters": [ "parameters": [
{ {
"parameter_type": { "parameter_type": {
"data": {
"Type": { "Type": {
"is_const": false, "is_const": false,
"scope": null, "scope": null,
"datatype": { "datatype": {
"data": {
"DataTypePrimType": { "DataTypePrimType": {
"prim_type": "Int32" "prim_type": "Int32"
} }
}, },
"span": {
"start": 64,
"end": 67
}
},
"modifiers": [] "modifiers": []
} }
}, },
"span": {
"start": 64,
"end": 67
}
},
"type_mod": null, "type_mod": null,
"identifier": "a", "identifier": "a",
"default": null "default": null
}, },
{ {
"parameter_type": { "parameter_type": {
"data": {
"Type": { "Type": {
"is_const": false, "is_const": false,
"scope": null, "scope": null,
"datatype": { "datatype": {
"data": {
"DataTypePrimType": { "DataTypePrimType": {
"prim_type": "Int32" "prim_type": "Int32"
} }
}, },
"span": {
"start": 71,
"end": 74
}
},
"modifiers": [] "modifiers": []
} }
}, },
"span": {
"start": 71,
"end": 74
}
},
"type_mod": null, "type_mod": null,
"identifier": "b", "identifier": "b",
"default": null "default": null
@ -150,51 +226,104 @@ fn integration_class_with_many_statements() {
] ]
} }
}, },
"span": {
"start": 63,
"end": 77
}
},
"is_const": false, "is_const": false,
"func_attr": 0, "func_attr": 0,
"block": { "block": {
"data": {
"StatBlock": { "StatBlock": {
"statements": [ "statements": [
{ {
"data": {
"Assignment": { "Assignment": {
"left": { "left": {
"data": {
"VarAccess": { "VarAccess": {
"scope": null, "scope": null,
"identifier": "_a" "identifier": "_a"
} }
}, },
"span": {
"start": 88,
"end": 90
}
},
"operator": "Assignment", "operator": "Assignment",
"right": { "right": {
"data": {
"VarAccess": { "VarAccess": {
"scope": null, "scope": null,
"identifier": "a" "identifier": "a"
} }
},
"span": {
"start": 93,
"end": 94
}
} }
} }
}, },
"span": {
"start": 88,
"end": 94
}
},
{ {
"data": {
"Assignment": { "Assignment": {
"left": { "left": {
"data": {
"VarAccess": { "VarAccess": {
"scope": null, "scope": null,
"identifier": "_b" "identifier": "_b"
} }
}, },
"span": {
"start": 104,
"end": 106
}
},
"operator": "Assignment", "operator": "Assignment",
"right": { "right": {
"data": {
"VarAccess": { "VarAccess": {
"scope": null, "scope": null,
"identifier": "b" "identifier": "b"
} }
} },
} "span": {
} "start": 109,
] "end": 110
} }
} }
} }
}, },
"span": {
"start": 104,
"end": 110
}
}
]
}
},
"span": {
"start": 78,
"end": 117
}
}
}
},
"span": {
"start": 57,
"end": 117
}
},
{ {
"data": {
"FuncDeclaration": { "FuncDeclaration": {
"modifiers": 0, "modifiers": 0,
"field_mod": null, "field_mod": null,
@ -202,44 +331,86 @@ fn integration_class_with_many_statements() {
"is_constructor": false, "is_constructor": false,
"returns_reference": false, "returns_reference": false,
"return_type": { "return_type": {
"data": {
"Type": { "Type": {
"is_const": false, "is_const": false,
"scope": null, "scope": null,
"datatype": { "datatype": {
"data": {
"DataTypePrimType": { "DataTypePrimType": {
"prim_type": "Int32" "prim_type": "Int32"
} }
}, },
"span": {
"start": 123,
"end": 126
}
},
"modifiers": [] "modifiers": []
} }
}, },
"span": {
"start": 123,
"end": 126
}
},
"name": "GetA", "name": "GetA",
"param_list": { "param_list": {
"data": {
"ParamList": { "ParamList": {
"parameters": [] "parameters": []
} }
}, },
"span": {
"start": 131,
"end": 133
}
},
"is_const": false, "is_const": false,
"func_attr": 0, "func_attr": 0,
"block": { "block": {
"data": {
"StatBlock": { "StatBlock": {
"statements": [ "statements": [
{ {
"data": {
"ReturnStatement": { "ReturnStatement": {
"expression": { "expression": {
"data": {
"VarAccess": { "VarAccess": {
"scope": null, "scope": null,
"identifier": "_a" "identifier": "_a"
} }
} },
} "span": {
} "start": 150,
] "end": 152
} }
} }
} }
}, },
"span": {
"start": 143,
"end": 153
}
}
]
}
},
"span": {
"start": 133,
"end": 159
}
}
}
},
"span": {
"start": 123,
"end": 159
}
},
{ {
"data": {
"FuncDeclaration": { "FuncDeclaration": {
"modifiers": 0, "modifiers": 0,
"field_mod": null, "field_mod": null,
@ -247,87 +418,174 @@ fn integration_class_with_many_statements() {
"is_constructor": false, "is_constructor": false,
"returns_reference": false, "returns_reference": false,
"return_type": { "return_type": {
"data": {
"Type": { "Type": {
"is_const": false, "is_const": false,
"scope": null, "scope": null,
"datatype": { "datatype": {
"data": {
"DataTypePrimType": { "DataTypePrimType": {
"prim_type": "Int32" "prim_type": "Int32"
} }
}, },
"span": {
"start": 165,
"end": 168
}
},
"modifiers": [] "modifiers": []
} }
}, },
"span": {
"start": 165,
"end": 168
}
},
"name": "Add", "name": "Add",
"param_list": { "param_list": {
"data": {
"ParamList": { "ParamList": {
"parameters": [] "parameters": []
} }
}, },
"span": {
"start": 172,
"end": 174
}
},
"is_const": false, "is_const": false,
"func_attr": 0, "func_attr": 0,
"block": { "block": {
"data": {
"StatBlock": { "StatBlock": {
"statements": [ "statements": [
{ {
"data": {
"ReturnStatement": { "ReturnStatement": {
"expression": { "expression": {
"data": {
"BinaryExpr": { "BinaryExpr": {
"left": { "left": {
"data": {
"VarAccess": { "VarAccess": {
"scope": null, "scope": null,
"identifier": "_a" "identifier": "_a"
} }
}, },
"span": {
"start": 192,
"end": 194
}
},
"operator": "Addition", "operator": "Addition",
"right": { "right": {
"data": {
"VarAccess": { "VarAccess": {
"scope": null, "scope": null,
"identifier": "_b" "identifier": "_b"
} }
} },
} "span": {
} "start": 197,
} "end": 199
}
]
} }
} }
} }
}, },
"span": {
"start": 192,
"end": 199
}
}
}
},
"span": {
"start": 185,
"end": 200
}
}
]
}
},
"span": {
"start": 175,
"end": 206
}
}
}
},
"span": {
"start": 165,
"end": 206
}
},
{ {
"data": {
"ClassDeclaration": { "ClassDeclaration": {
"modifiers": 0, "modifiers": 0,
"name": "Inner", "name": "Inner",
"inherits": [], "inherits": [],
"statements": [ "statements": [
{ {
"data": {
"Var": { "Var": {
"modifier": 0, "modifier": 0,
"var_type": { "var_type": {
"data": {
"Type": { "Type": {
"is_const": false, "is_const": false,
"scope": null, "scope": null,
"datatype": { "datatype": {
"data": {
"DataTypePrimType": { "DataTypePrimType": {
"prim_type": "Int32" "prim_type": "Int32"
} }
}, },
"span": {
"start": 234,
"end": 237
}
},
"modifiers": [] "modifiers": []
} }
}, },
"span": {
"start": 234,
"end": 237
}
},
"identifier": "a", "identifier": "a",
"assignment": null "assignment": null
} }
} },
] "span": {
"start": 234,
"end": 240
} }
} }
] ]
} }
},
"span": {
"start": 212,
"end": 246
}
} }
] ]
} }
},
"span": {
"start": 0,
"end": 248
}
}
]
}
},
"span": {
"start": 0,
"end": 248
}
}"#).unwrap(); }"#).unwrap();
assert_eq!(parsed_tree, expected_tree); assert_eq!(parsed_tree, expected_tree);
} }

View File

@ -79,19 +79,32 @@ fn integration_empty_class_declaration() {
println!("Parsed Tree JSON: {}", serde_json::to_string(&parsed_tree).unwrap()); println!("Parsed Tree JSON: {}", serde_json::to_string(&parsed_tree).unwrap());
let expected_tree: Box<ParsedStatement> = let expected_tree: Box<ParsedStatement> =
serde_json::from_str(r#"{ serde_json::from_str(r#"{
"data": {
"Script": { "Script": {
"statements": [ "statements": [
{ {
"data": {
"ClassDeclaration": { "ClassDeclaration": {
"modifiers": 0, "modifiers": 0,
"name": "Foo", "name": "Foo",
"inherits": [], "inherits": [],
"statements": [] "statements": []
} }
},
"span": {
"start": 0,
"end": 12
}
} }
] ]
} }
}"#).unwrap(); },
"span": {
"start": 0,
"end": 12
}
}
"#).unwrap();
assert_eq!(parsed_tree, expected_tree); assert_eq!(parsed_tree, expected_tree);
} }
// A substring of a script should never panic, even though it might be completely invalid. // A substring of a script should never panic, even though it might be completely invalid.

View File

@ -398,17 +398,25 @@ fn integration_enum_definition() {
println!("Parsed Tree JSON: {}", serde_json::to_string(&parsed_tree).unwrap()); println!("Parsed Tree JSON: {}", serde_json::to_string(&parsed_tree).unwrap());
let expected_tree: Box<ParsedStatement> = let expected_tree: Box<ParsedStatement> =
serde_json::from_str(r#"{ serde_json::from_str(r#"{
"data": {
"Script": { "Script": {
"statements": [ "statements": [
{ {
"data": {
"EnumDeclaration": { "EnumDeclaration": {
"modifiers": 0, "modifiers": 0,
"identifier": "TestEnum", "identifier": "TestEnum",
"base_type": { "base_type": {
"data": {
"DataTypePrimType": { "DataTypePrimType": {
"prim_type": "UInt8" "prim_type": "UInt8"
} }
}, },
"span": {
"start": 16,
"end": 21
}
},
"values": [ "values": [
[ [
"a", "a",
@ -425,7 +433,13 @@ fn integration_enum_definition() {
[ [
"d", "d",
{ {
"data": {
"IntegerLiteral": 128 "IntegerLiteral": 128
},
"span": {
"start": 53,
"end": 56
}
} }
], ],
[ [
@ -434,9 +448,19 @@ fn integration_enum_definition() {
] ]
] ]
} }
},
"span": {
"start": 0,
"end": 65
}
} }
] ]
} }
},
"span": {
"start": 0,
"end": 65
}
}"#).unwrap(); }"#).unwrap();
assert_eq!(parsed_tree, expected_tree); assert_eq!(parsed_tree, expected_tree);
} }

View File

@ -1,6 +1,7 @@
//////////////////////////// ////////////////////////////
// Automatically Generated// // Automatically Generated//
////////////////////////////mod enum_definition; ////////////////////////////
mod enum_definition;
mod multiple_inheritance_class; mod multiple_inheritance_class;
mod empty_class_declaration; mod empty_class_declaration;
mod class_with_many_statements; mod class_with_many_statements;

View File

@ -155,9 +155,11 @@ fn integration_multiple_inheritance_class() {
println!("Parsed Tree JSON: {}", serde_json::to_string(&parsed_tree).unwrap()); println!("Parsed Tree JSON: {}", serde_json::to_string(&parsed_tree).unwrap());
let expected_tree: Box<ParsedStatement> = let expected_tree: Box<ParsedStatement> =
serde_json::from_str(r#"{ serde_json::from_str(r#"{
"data": {
"Script": { "Script": {
"statements": [ "statements": [
{ {
"data": {
"ClassDeclaration": { "ClassDeclaration": {
"modifiers": 0, "modifiers": 0,
"name": "Foo", "name": "Foo",
@ -168,10 +170,21 @@ fn integration_multiple_inheritance_class() {
], ],
"statements": [] "statements": []
} }
},
"span": {
"start": 0,
"end": 28
}
} }
] ]
} }
}"#).unwrap(); },
"span": {
"start": 0,
"end": 28
}
}
"#).unwrap();
assert_eq!(parsed_tree, expected_tree); assert_eq!(parsed_tree, expected_tree);
} }
// A substring of a script should never panic, even though it might be completely invalid. // A substring of a script should never panic, even though it might be completely invalid.

View File

@ -1,7 +1,9 @@
{ {
"data": {
"Script": { "Script": {
"statements": [ "statements": [
{ {
"data": {
"FuncDeclaration": { "FuncDeclaration": {
"modifiers": 0, "modifiers": 0,
"field_mod": null, "field_mod": null,
@ -9,51 +11,88 @@
"is_constructor": false, "is_constructor": false,
"returns_reference": false, "returns_reference": false,
"return_type": { "return_type": {
"data": {
"Type": { "Type": {
"is_const": false, "is_const": false,
"scope": null, "scope": null,
"datatype": { "datatype": {
"data": {
"DataTypePrimType": { "DataTypePrimType": {
"prim_type": "Int32" "prim_type": "Int32"
} }
}, },
"span": {
"start": 0,
"end": 3
}
},
"modifiers": [] "modifiers": []
} }
}, },
"span": {
"start": 0,
"end": 3
}
},
"name": "add", "name": "add",
"param_list": { "param_list": {
"data": {
"ParamList": { "ParamList": {
"parameters": [ "parameters": [
{ {
"parameter_type": { "parameter_type": {
"data": {
"Type": { "Type": {
"is_const": false, "is_const": false,
"scope": null, "scope": null,
"datatype": { "datatype": {
"data": {
"DataTypePrimType": { "DataTypePrimType": {
"prim_type": "Int32" "prim_type": "Int32"
} }
}, },
"span": {
"start": 8,
"end": 11
}
},
"modifiers": [] "modifiers": []
} }
}, },
"span": {
"start": 8,
"end": 11
}
},
"type_mod": null, "type_mod": null,
"identifier": "a", "identifier": "a",
"default": null "default": null
}, },
{ {
"parameter_type": { "parameter_type": {
"data": {
"Type": { "Type": {
"is_const": false, "is_const": false,
"scope": null, "scope": null,
"datatype": { "datatype": {
"data": {
"DataTypePrimType": { "DataTypePrimType": {
"prim_type": "Int32" "prim_type": "Int32"
} }
}, },
"span": {
"start": 15,
"end": 18
}
},
"modifiers": [] "modifiers": []
} }
}, },
"span": {
"start": 15,
"end": 18
}
},
"type_mod": null, "type_mod": null,
"identifier": "b", "identifier": "b",
"default": null "default": null
@ -61,37 +100,82 @@
] ]
} }
}, },
"span": {
"start": 7,
"end": 21
}
},
"is_const": false, "is_const": false,
"func_attr": 0, "func_attr": 0,
"block": { "block": {
"data": {
"StatBlock": { "StatBlock": {
"statements": [ "statements": [
{ {
"data": {
"ReturnStatement": { "ReturnStatement": {
"expression": { "expression": {
"data": {
"BinaryExpr": { "BinaryExpr": {
"left": { "left": {
"data": {
"VarAccess": { "VarAccess": {
"scope": null, "scope": null,
"identifier": "a" "identifier": "a"
} }
}, },
"span": {
"start": 35,
"end": 36
}
},
"operator": "Addition", "operator": "Addition",
"right": { "right": {
"data": {
"VarAccess": { "VarAccess": {
"scope": null, "scope": null,
"identifier": "b" "identifier": "b"
} }
},
"span": {
"start": 39,
"end": 40
} }
} }
} }
},
"span": {
"start": 35,
"end": 40
}
}
}
},
"span": {
"start": 28,
"end": 41
}
}
]
}
},
"span": {
"start": 22,
"end": 43
}
}
}
},
"span": {
"start": 0,
"end": 43
} }
} }
] ]
} }
} },
} "span": {
} "start": 0,
] "end": 43
} }
} }

View File

@ -1,51 +1,90 @@
{ {
"data": {
"Script": { "Script": {
"statements": [ "statements": [
{ {
"data": {
"ClassDeclaration": { "ClassDeclaration": {
"modifiers": 10, "modifiers": 10,
"name": "Foobar", "name": "Foobar",
"inherits": [], "inherits": [],
"statements": [ "statements": [
{ {
"data": {
"Var": { "Var": {
"modifier": 0, "modifier": 0,
"var_type": { "var_type": {
"data": {
"Type": { "Type": {
"is_const": false, "is_const": false,
"scope": null, "scope": null,
"datatype": { "datatype": {
"data": {
"DataTypePrimType": { "DataTypePrimType": {
"prim_type": "Int32" "prim_type": "Int32"
} }
}, },
"span": {
"start": 32,
"end": 35
}
},
"modifiers": [] "modifiers": []
} }
}, },
"span": {
"start": 32,
"end": 35
}
},
"identifier": "_a", "identifier": "_a",
"assignment": null "assignment": null
} }
}, },
"span": {
"start": 32,
"end": 39
}
},
{ {
"data": {
"Var": { "Var": {
"modifier": 0, "modifier": 0,
"var_type": { "var_type": {
"data": {
"Type": { "Type": {
"is_const": false, "is_const": false,
"scope": null, "scope": null,
"datatype": { "datatype": {
"data": {
"DataTypePrimType": { "DataTypePrimType": {
"prim_type": "Int32" "prim_type": "Int32"
} }
}, },
"span": {
"start": 44,
"end": 47
}
},
"modifiers": [] "modifiers": []
} }
}, },
"span": {
"start": 44,
"end": 47
}
},
"identifier": "_b", "identifier": "_b",
"assignment": null "assignment": null
} }
}, },
"span": {
"start": 44,
"end": 51
}
},
{ {
"data": {
"FuncDeclaration": { "FuncDeclaration": {
"modifiers": 0, "modifiers": 0,
"field_mod": null, "field_mod": null,
@ -53,51 +92,88 @@
"is_constructor": true, "is_constructor": true,
"returns_reference": false, "returns_reference": false,
"return_type": { "return_type": {
"data": {
"Type": { "Type": {
"is_const": false, "is_const": false,
"scope": null, "scope": null,
"datatype": { "datatype": {
"data": {
"DataTypeIdentifier": { "DataTypeIdentifier": {
"identifier": "Foobar" "identifier": "Foobar"
} }
}, },
"span": {
"start": 57,
"end": 63
}
},
"modifiers": [] "modifiers": []
} }
}, },
"span": {
"start": 57,
"end": 63
}
},
"name": "Foobar", "name": "Foobar",
"param_list": { "param_list": {
"data": {
"ParamList": { "ParamList": {
"parameters": [ "parameters": [
{ {
"parameter_type": { "parameter_type": {
"data": {
"Type": { "Type": {
"is_const": false, "is_const": false,
"scope": null, "scope": null,
"datatype": { "datatype": {
"data": {
"DataTypePrimType": { "DataTypePrimType": {
"prim_type": "Int32" "prim_type": "Int32"
} }
}, },
"span": {
"start": 64,
"end": 67
}
},
"modifiers": [] "modifiers": []
} }
}, },
"span": {
"start": 64,
"end": 67
}
},
"type_mod": null, "type_mod": null,
"identifier": "a", "identifier": "a",
"default": null "default": null
}, },
{ {
"parameter_type": { "parameter_type": {
"data": {
"Type": { "Type": {
"is_const": false, "is_const": false,
"scope": null, "scope": null,
"datatype": { "datatype": {
"data": {
"DataTypePrimType": { "DataTypePrimType": {
"prim_type": "Int32" "prim_type": "Int32"
} }
}, },
"span": {
"start": 71,
"end": 74
}
},
"modifiers": [] "modifiers": []
} }
}, },
"span": {
"start": 71,
"end": 74
}
},
"type_mod": null, "type_mod": null,
"identifier": "b", "identifier": "b",
"default": null "default": null
@ -105,51 +181,104 @@
] ]
} }
}, },
"span": {
"start": 63,
"end": 77
}
},
"is_const": false, "is_const": false,
"func_attr": 0, "func_attr": 0,
"block": { "block": {
"data": {
"StatBlock": { "StatBlock": {
"statements": [ "statements": [
{ {
"data": {
"Assignment": { "Assignment": {
"left": { "left": {
"data": {
"VarAccess": { "VarAccess": {
"scope": null, "scope": null,
"identifier": "_a" "identifier": "_a"
} }
}, },
"span": {
"start": 88,
"end": 90
}
},
"operator": "Assignment", "operator": "Assignment",
"right": { "right": {
"data": {
"VarAccess": { "VarAccess": {
"scope": null, "scope": null,
"identifier": "a" "identifier": "a"
} }
},
"span": {
"start": 93,
"end": 94
}
} }
} }
}, },
"span": {
"start": 88,
"end": 94
}
},
{ {
"data": {
"Assignment": { "Assignment": {
"left": { "left": {
"data": {
"VarAccess": { "VarAccess": {
"scope": null, "scope": null,
"identifier": "_b" "identifier": "_b"
} }
}, },
"span": {
"start": 104,
"end": 106
}
},
"operator": "Assignment", "operator": "Assignment",
"right": { "right": {
"data": {
"VarAccess": { "VarAccess": {
"scope": null, "scope": null,
"identifier": "b" "identifier": "b"
} }
} },
} "span": {
} "start": 109,
] "end": 110
} }
} }
} }
}, },
"span": {
"start": 104,
"end": 110
}
}
]
}
},
"span": {
"start": 78,
"end": 117
}
}
}
},
"span": {
"start": 57,
"end": 117
}
},
{ {
"data": {
"FuncDeclaration": { "FuncDeclaration": {
"modifiers": 0, "modifiers": 0,
"field_mod": null, "field_mod": null,
@ -157,44 +286,86 @@
"is_constructor": false, "is_constructor": false,
"returns_reference": false, "returns_reference": false,
"return_type": { "return_type": {
"data": {
"Type": { "Type": {
"is_const": false, "is_const": false,
"scope": null, "scope": null,
"datatype": { "datatype": {
"data": {
"DataTypePrimType": { "DataTypePrimType": {
"prim_type": "Int32" "prim_type": "Int32"
} }
}, },
"span": {
"start": 123,
"end": 126
}
},
"modifiers": [] "modifiers": []
} }
}, },
"span": {
"start": 123,
"end": 126
}
},
"name": "GetA", "name": "GetA",
"param_list": { "param_list": {
"data": {
"ParamList": { "ParamList": {
"parameters": [] "parameters": []
} }
}, },
"span": {
"start": 131,
"end": 133
}
},
"is_const": false, "is_const": false,
"func_attr": 0, "func_attr": 0,
"block": { "block": {
"data": {
"StatBlock": { "StatBlock": {
"statements": [ "statements": [
{ {
"data": {
"ReturnStatement": { "ReturnStatement": {
"expression": { "expression": {
"data": {
"VarAccess": { "VarAccess": {
"scope": null, "scope": null,
"identifier": "_a" "identifier": "_a"
} }
} },
} "span": {
} "start": 150,
] "end": 152
} }
} }
} }
}, },
"span": {
"start": 143,
"end": 153
}
}
]
}
},
"span": {
"start": 133,
"end": 159
}
}
}
},
"span": {
"start": 123,
"end": 159
}
},
{ {
"data": {
"FuncDeclaration": { "FuncDeclaration": {
"modifiers": 0, "modifiers": 0,
"field_mod": null, "field_mod": null,
@ -202,85 +373,172 @@
"is_constructor": false, "is_constructor": false,
"returns_reference": false, "returns_reference": false,
"return_type": { "return_type": {
"data": {
"Type": { "Type": {
"is_const": false, "is_const": false,
"scope": null, "scope": null,
"datatype": { "datatype": {
"data": {
"DataTypePrimType": { "DataTypePrimType": {
"prim_type": "Int32" "prim_type": "Int32"
} }
}, },
"span": {
"start": 165,
"end": 168
}
},
"modifiers": [] "modifiers": []
} }
}, },
"span": {
"start": 165,
"end": 168
}
},
"name": "Add", "name": "Add",
"param_list": { "param_list": {
"data": {
"ParamList": { "ParamList": {
"parameters": [] "parameters": []
} }
}, },
"span": {
"start": 172,
"end": 174
}
},
"is_const": false, "is_const": false,
"func_attr": 0, "func_attr": 0,
"block": { "block": {
"data": {
"StatBlock": { "StatBlock": {
"statements": [ "statements": [
{ {
"data": {
"ReturnStatement": { "ReturnStatement": {
"expression": { "expression": {
"data": {
"BinaryExpr": { "BinaryExpr": {
"left": { "left": {
"data": {
"VarAccess": { "VarAccess": {
"scope": null, "scope": null,
"identifier": "_a" "identifier": "_a"
} }
}, },
"span": {
"start": 192,
"end": 194
}
},
"operator": "Addition", "operator": "Addition",
"right": { "right": {
"data": {
"VarAccess": { "VarAccess": {
"scope": null, "scope": null,
"identifier": "_b" "identifier": "_b"
} }
} },
} "span": {
} "start": 197,
} "end": 199
}
]
} }
} }
} }
}, },
"span": {
"start": 192,
"end": 199
}
}
}
},
"span": {
"start": 185,
"end": 200
}
}
]
}
},
"span": {
"start": 175,
"end": 206
}
}
}
},
"span": {
"start": 165,
"end": 206
}
},
{ {
"data": {
"ClassDeclaration": { "ClassDeclaration": {
"modifiers": 0, "modifiers": 0,
"name": "Inner", "name": "Inner",
"inherits": [], "inherits": [],
"statements": [ "statements": [
{ {
"data": {
"Var": { "Var": {
"modifier": 0, "modifier": 0,
"var_type": { "var_type": {
"data": {
"Type": { "Type": {
"is_const": false, "is_const": false,
"scope": null, "scope": null,
"datatype": { "datatype": {
"data": {
"DataTypePrimType": { "DataTypePrimType": {
"prim_type": "Int32" "prim_type": "Int32"
} }
}, },
"span": {
"start": 234,
"end": 237
}
},
"modifiers": [] "modifiers": []
} }
}, },
"span": {
"start": 234,
"end": 237
}
},
"identifier": "a", "identifier": "a",
"assignment": null "assignment": null
} }
} },
] "span": {
"start": 234,
"end": 240
} }
} }
] ]
} }
},
"span": {
"start": 212,
"end": 246
}
} }
] ]
} }
},
"span": {
"start": 0,
"end": 248
}
}
]
}
},
"span": {
"start": 0,
"end": 248
}
} }

View File

@ -1,14 +1,26 @@
{ {
"data": {
"Script": { "Script": {
"statements": [ "statements": [
{ {
"data": {
"ClassDeclaration": { "ClassDeclaration": {
"modifiers": 0, "modifiers": 0,
"name": "Foo", "name": "Foo",
"inherits": [], "inherits": [],
"statements": [] "statements": []
} }
},
"span": {
"start": 0,
"end": 12
}
} }
] ]
} }
},
"span": {
"start": 0,
"end": 12
}
} }

View File

@ -1,15 +1,23 @@
{ {
"data": {
"Script": { "Script": {
"statements": [ "statements": [
{ {
"data": {
"EnumDeclaration": { "EnumDeclaration": {
"modifiers": 0, "modifiers": 0,
"identifier": "TestEnum", "identifier": "TestEnum",
"base_type": { "base_type": {
"data": {
"DataTypePrimType": { "DataTypePrimType": {
"prim_type": "UInt8" "prim_type": "UInt8"
} }
}, },
"span": {
"start": 16,
"end": 21
}
},
"values": [ "values": [
[ [
"a", "a",
@ -26,7 +34,13 @@
[ [
"d", "d",
{ {
"data": {
"IntegerLiteral": 128 "IntegerLiteral": 128
},
"span": {
"start": 53,
"end": 56
}
} }
], ],
[ [
@ -35,7 +49,17 @@
] ]
] ]
} }
},
"span": {
"start": 0,
"end": 65
}
} }
] ]
} }
},
"span": {
"start": 0,
"end": 65
}
} }

View File

@ -1,7 +1,9 @@
{ {
"data": {
"Script": { "Script": {
"statements": [ "statements": [
{ {
"data": {
"ClassDeclaration": { "ClassDeclaration": {
"modifiers": 0, "modifiers": 0,
"name": "Foo", "name": "Foo",
@ -12,7 +14,17 @@
], ],
"statements": [] "statements": []
} }
},
"span": {
"start": 0,
"end": 28
}
} }
] ]
} }
},
"span": {
"start": 0,
"end": 28
}
} }

View File

@ -4,15 +4,18 @@
#![feature(fn_traits)] #![feature(fn_traits)]
#![feature(iter_advance_by)] #![feature(iter_advance_by)]
#![feature(backtrace)] #![feature(backtrace)]
#![feature(in_band_lifetimes)]
extern crate core; extern crate core;
#[cfg(test)]
mod integration_tests;
pub mod binding;
pub mod defines; pub mod defines;
pub mod logger; pub mod logger;
pub mod modifiers; pub mod modifiers;
pub mod parsing; pub mod parsing;
pub(crate) mod prim_type; pub(crate) mod prim_type;
pub mod span; pub mod span;
pub mod types;
#[cfg(test)]
mod integration_tests;

File diff suppressed because it is too large Load Diff

View File

@ -9,6 +9,7 @@ use crate::parsing::parser::parser_operators::{
use crate::prim_type::PrimitiveType; use crate::prim_type::PrimitiveType;
use enumflags2::BitFlags; use enumflags2::BitFlags;
use crate::span::Span;
#[cfg(test)] #[cfg(test)]
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
@ -23,10 +24,17 @@ pub struct ParsedParameter {
#[derive(PartialEq, Debug)] #[derive(PartialEq, Debug)]
#[cfg_attr(test, derive(Serialize, Deserialize))] #[cfg_attr(test, derive(Serialize, Deserialize))]
pub enum ParsedStatement { pub struct ParsedStatement {
pub data: ParsedStatementData,
pub span: Span,
}
#[derive(PartialEq, Debug)]
#[cfg_attr(test, derive(Serialize, Deserialize))]
pub enum ParsedStatementData {
Invalid, Invalid,
Script { Script {
statements: Vec<ParsedStatement>, statements: Vec<Box<ParsedStatement>>,
}, },
Namespace { Namespace {
identifier: String, identifier: String,
@ -36,12 +44,12 @@ pub enum ParsedStatement {
type_mod: BitFlags<TypeModifier>, type_mod: BitFlags<TypeModifier>,
identifier: String, identifier: String,
inherits: Vec<String>, inherits: Vec<String>,
statements: Vec<ParsedStatement>, statements: Vec<Box<ParsedStatement>>,
}, },
Scope { Scope {
is_global: bool, is_global: bool,
scope: Vec<String>, scope: Vec<String>,
generic_types: Option<Vec<ParsedStatement>>, generic_types: Option<Vec<Box<ParsedStatement>>>,
}, },
VirtProp { VirtProp {
field_mod: Option<FieldModifier>, field_mod: Option<FieldModifier>,
@ -60,7 +68,7 @@ pub enum ParsedStatement {
DataTypeIdentifier { DataTypeIdentifier {
identifier: String, identifier: String,
}, },
DataTypeAuto {}, DataTypeAuto,
DataTypePrimType { DataTypePrimType {
prim_type: PrimitiveType, prim_type: PrimitiveType,
}, },
@ -90,7 +98,7 @@ pub enum ParsedStatement {
}, },
ExprVoidValue {}, ExprVoidValue {},
ConstructCall { ConstructCall {
statement_type: Option<Box<ParsedStatement>>, statement_type: Box<ParsedStatement>,
arglist: Box<ParsedStatement>, arglist: Box<ParsedStatement>,
}, },
FuncCall { FuncCall {
@ -176,8 +184,8 @@ pub enum ParsedStatement {
statement: Box<ParsedStatement>, statement: Box<ParsedStatement>,
else_statement: Option<Box<ParsedStatement>>, else_statement: Option<Box<ParsedStatement>>,
}, },
BreakStatement {}, BreakStatement,
ContinueStatement {}, ContinueStatement,
ReturnStatement { ReturnStatement {
expression: Option<Box<ParsedStatement>>, expression: Option<Box<ParsedStatement>>,
}, },

View File

@ -1,8 +1,9 @@
use super::parse; use super::parse;
use super::parsed_statement::ParsedStatement; use super::parsed_statement::ParsedStatement;
use crate::modifiers::TypeModifier; use crate::modifiers::TypeModifier;
use crate::parsing::lexer::lex_tokens::TokenType::AutoKeyword;
use crate::parsing::lexer::lex_tokens::{LexToken, TokenType}; use crate::parsing::lexer::lex_tokens::{LexToken, TokenType};
use crate::parsing::parser::parsed_statement::ParsedStatement::DataTypeAuto; use crate::parsing::parser::parsed_statement::ParsedStatementData;
use crate::parsing::parser::parser_operators::{BinaryOperator, PreOperator}; use crate::parsing::parser::parser_operators::{BinaryOperator, PreOperator};
use crate::span::Span; use crate::span::Span;
@ -33,11 +34,12 @@ fn test_empty_namespace() {
std::panic::panic_any(_message.stringify()); std::panic::panic_any(_message.stringify());
}, },
); );
if let ParsedStatement::Script { statements } = script.as_ref() { if let ParsedStatementData::Script { statements } = &script.as_ref().data {
assert_eq!(1, statements.len()); assert_eq!(1, statements.len());
if let ParsedStatement::Namespace { identifier, script } = &statements[0] { if let ParsedStatementData::Namespace { identifier, script } = &statements[0].as_ref().data
{
assert_eq!(identifier, "foo"); assert_eq!(identifier, "foo");
if let ParsedStatement::Script { statements } = script.as_ref() { if let ParsedStatementData::Script { statements } = &script.as_ref().data {
assert_eq!(0, statements.len()); assert_eq!(0, statements.len());
} else { } else {
unreachable!(); unreachable!();
@ -66,14 +68,14 @@ fn test_empty_interface() {
std::panic::panic_any(_message.stringify()); std::panic::panic_any(_message.stringify());
}, },
); );
if let ParsedStatement::Script { statements } = script.as_ref() { if let ParsedStatementData::Script { statements } = &script.as_ref().data {
assert_eq!(1, statements.len()); assert_eq!(1, statements.len());
if let ParsedStatement::Interface { if let ParsedStatementData::Interface {
type_mod, type_mod,
identifier, identifier,
inherits, inherits,
statements, statements,
} = &statements[0] } = &statements[0].as_ref().data
{ {
assert!(type_mod.is_empty()); assert!(type_mod.is_empty());
assert_eq!(identifier, "foo"); assert_eq!(identifier, "foo");
@ -105,14 +107,14 @@ fn test_empty_external_shared_interface() {
std::panic::panic_any(_message.stringify()); std::panic::panic_any(_message.stringify());
}, },
); );
if let ParsedStatement::Script { statements } = script.as_ref() { if let ParsedStatementData::Script { statements } = &script.as_ref().data {
assert_eq!(1, statements.len()); assert_eq!(1, statements.len());
if let ParsedStatement::Interface { if let ParsedStatementData::Interface {
type_mod, type_mod,
identifier, identifier,
inherits, inherits,
statements, statements,
} = &statements[0] } = &statements[0].as_ref().data
{ {
assert!(!type_mod.is_empty()); assert!(!type_mod.is_empty());
assert!(type_mod.contains(TypeModifier::External)); assert!(type_mod.contains(TypeModifier::External));
@ -152,20 +154,20 @@ fn test_interface_with_virtprop() {
std::panic::panic_any(_message.stringify()); std::panic::panic_any(_message.stringify());
}, },
); );
if let ParsedStatement::Script { statements } = script.as_ref() { if let ParsedStatementData::Script { statements } = &script.as_ref().data {
assert_eq!(1, statements.len()); assert_eq!(1, statements.len());
if let ParsedStatement::Interface { if let ParsedStatementData::Interface {
type_mod, type_mod,
identifier, identifier,
inherits, inherits,
statements, statements,
} = &statements[0] } = &statements[0].as_ref().data
{ {
assert!(type_mod.is_empty()); assert!(type_mod.is_empty());
assert_eq!(identifier, "foo"); assert_eq!(identifier, "foo");
assert_eq!(inherits.len(), 0); assert_eq!(inherits.len(), 0);
assert_eq!(statements.len(), 1); assert_eq!(statements.len(), 1);
if let ParsedStatement::VirtProp { if let ParsedStatementData::VirtProp {
field_mod, field_mod,
property_type, property_type,
identifier, identifier,
@ -176,16 +178,16 @@ fn test_interface_with_virtprop() {
has_set, has_set,
is_set_const, is_set_const,
set_statement, set_statement,
} = &statements[0] } = &statements[0].as_ref().data
{ {
assert_eq!(*field_mod, None); assert_eq!(*field_mod, None);
if let ParsedStatement::Type { if let ParsedStatementData::Type {
is_const, datatype, .. is_const, datatype, ..
} = property_type.as_ref() } = &property_type.as_ref().data
{ {
assert!(!is_const); assert!(!is_const);
if let ParsedStatement::DataTypeIdentifier { identifier, .. } = if let ParsedStatementData::DataTypeIdentifier { identifier, .. } =
datatype.as_ref() &datatype.as_ref().data
{ {
assert_eq!(identifier, "bar"); assert_eq!(identifier, "bar");
} else { } else {
@ -231,30 +233,33 @@ fn test_assign_to_global_variable() {
std::panic::panic_any(_message.stringify()); std::panic::panic_any(_message.stringify());
}, },
); );
if let ParsedStatement::Script { statements } = script.as_ref() { if let ParsedStatementData::Script { statements } = &script.as_ref().data {
assert_eq!(1, statements.len()); assert_eq!(1, statements.len());
if let ParsedStatement::Var { if let ParsedStatementData::Var {
modifier, modifier,
var_type, var_type,
identifier, identifier,
assignment, assignment,
} = &statements[0] } = &statements[0].as_ref().data
{ {
assert!(modifier.is_empty()); assert!(modifier.is_empty());
assert_eq!( assert_eq!(
*var_type.as_ref(), var_type.as_ref().data,
ParsedStatement::Type { ParsedStatementData::Type {
is_const: false, is_const: false,
scope: None, scope: None,
datatype: Box::from(DataTypeAuto {}), datatype: Box::from(ParsedStatement {
data: ParsedStatementData::DataTypeAuto,
span: Span { start: 0, end: 0 }
}),
modifiers: vec![] modifiers: vec![]
} }
); );
assert_eq!(identifier, "foo"); assert_eq!(identifier, "foo");
assert!(assignment.is_some()); assert!(assignment.is_some());
assert_eq!( assert_eq!(
*(assignment.as_ref().unwrap().as_ref()), (assignment.as_ref().unwrap().as_ref()).data,
ParsedStatement::IntegerLiteral(100) ParsedStatementData::IntegerLiteral(100)
); );
} else { } else {
unreachable!() unreachable!()
@ -283,32 +288,38 @@ fn test_assign_negative_to_global_variable() {
std::panic::panic_any(_message.stringify()); std::panic::panic_any(_message.stringify());
}, },
); );
if let ParsedStatement::Script { statements } = script.as_ref() { if let ParsedStatementData::Script { statements } = &script.as_ref().data {
assert_eq!(1, statements.len()); assert_eq!(1, statements.len());
if let ParsedStatement::Var { if let ParsedStatementData::Var {
modifier, modifier,
var_type, var_type,
identifier, identifier,
assignment, assignment,
} = &statements[0] } = &statements[0].as_ref().data
{ {
assert!(modifier.is_empty()); assert!(modifier.is_empty());
assert_eq!( assert_eq!(
*var_type.as_ref(), var_type.as_ref().data,
ParsedStatement::Type { ParsedStatementData::Type {
is_const: false, is_const: false,
scope: None, scope: None,
datatype: Box::from(DataTypeAuto {}), datatype: Box::from(ParsedStatement {
data: ParsedStatementData::DataTypeAuto,
span: Span { start: 0, end: 0 }
}),
modifiers: vec![] modifiers: vec![]
} }
); );
assert_eq!(identifier, "foo"); assert_eq!(identifier, "foo");
assert!(assignment.is_some()); assert!(assignment.is_some());
assert_eq!( assert_eq!(
*(assignment.as_ref().unwrap().as_ref()), (assignment.as_ref().unwrap().as_ref()).data,
ParsedStatement::ExprPreOp { ParsedStatementData::ExprPreOp {
operator: PreOperator::Negative, operator: PreOperator::Negative,
operand: Box::from(ParsedStatement::IntegerLiteral(100)), operand: Box::from(ParsedStatement {
data: ParsedStatementData::IntegerLiteral(100),
span: Span { start: 0, end: 0 }
}),
} }
); );
} else { } else {
@ -340,35 +351,47 @@ fn test_assign_addition_to_global_variable() {
std::panic::panic_any(_message.stringify()); std::panic::panic_any(_message.stringify());
}, },
); );
if let ParsedStatement::Script { statements } = script.as_ref() { if let ParsedStatementData::Script { statements } = &script.as_ref().data {
assert_eq!(1, statements.len()); assert_eq!(1, statements.len());
if let ParsedStatement::Var { if let ParsedStatementData::Var {
modifier, modifier,
var_type, var_type,
identifier, identifier,
assignment, assignment,
} = &statements[0] } = &statements[0].as_ref().data
{ {
assert!(modifier.is_empty()); assert!(modifier.is_empty());
assert_eq!( assert_eq!(
*var_type.as_ref(), var_type.as_ref().data,
ParsedStatement::Type { ParsedStatementData::Type {
is_const: false, is_const: false,
scope: None, scope: None,
datatype: Box::from(DataTypeAuto {}), datatype: Box::from(ParsedStatement {
data: ParsedStatementData::DataTypeAuto,
span: Span { start: 0, end: 0 }
}),
modifiers: vec![] modifiers: vec![]
} }
); );
assert_eq!(identifier, "foo"); assert_eq!(identifier, "foo");
assert!(assignment.is_some()); assert!(assignment.is_some());
assert_eq!( assert_eq!(
*(assignment.as_ref().unwrap().as_ref()), (assignment.as_ref().unwrap().as_ref()).data,
ParsedStatement::BinaryExpr { ParsedStatementData::BinaryExpr {
left: Box::new(ParsedStatement::IntegerLiteral(100)), left: Box::new(ParsedStatement {
data: ParsedStatementData::IntegerLiteral(100),
span: Span { start: 0, end: 0 }
}),
operator: BinaryOperator::Addition, operator: BinaryOperator::Addition,
right: Box::new(ParsedStatement::ExprPreOp { right: Box::new(ParsedStatement {
data: ParsedStatementData::ExprPreOp {
operator: PreOperator::Negative, operator: PreOperator::Negative,
operand: Box::new(ParsedStatement::IntegerLiteral(20)), operand: Box::new(ParsedStatement {
data: ParsedStatementData::IntegerLiteral(20),
span: Span { start: 0, end: 0 }
}),
},
span: Span { start: 0, end: 0 }
}), }),
} }
); );

10
src/types/mod.rs Normal file
View File

@ -0,0 +1,10 @@
use crate::defines::PointerSize;
pub mod script_type;
pub mod script_type_namespace;
pub mod type_library;
pub trait Type {
fn get_size(&self) -> PointerSize;
fn is_enum(&self) -> bool;
}

33
src/types/script_type.rs Normal file
View File

@ -0,0 +1,33 @@
use crate::defines::PointerSize;
use crate::types::Type;
use std::sync::Arc;
pub struct ScriptType {
fields: Vec<ScriptTypeField>,
size: PointerSize,
is_enum: bool,
}
pub struct ScriptTypeField {
field_type: Arc<dyn Type>,
}
impl ScriptType {
pub fn new() -> ScriptType {
ScriptType {
fields: Vec::new(),
size: 0,
is_enum: false,
}
}
}
impl Type for ScriptType {
fn get_size(&self) -> PointerSize {
self.size
}
fn is_enum(&self) -> bool {
self.is_enum
}
}

View File

@ -0,0 +1,22 @@
use crate::types::script_type::ScriptType;
use std::collections::HashMap;
pub struct ScriptTypeNamespace {
types: HashMap<String, ScriptType>,
namespaces: HashMap<String, Box<ScriptTypeNamespace>>,
root_namespace: Option<Box<ScriptTypeNamespace>>,
}
impl ScriptTypeNamespace {
pub fn new<'b>(root: Option<Box<ScriptTypeNamespace>>) -> ScriptTypeNamespace {
ScriptTypeNamespace {
types: HashMap::new(),
namespaces: HashMap::new(),
root_namespace: root,
}
}
pub fn register(&mut self, identifier: &String, script_type: ScriptType) {
self.types.insert(identifier.to_string(), script_type);
}
}

22
src/types/type_library.rs Normal file
View File

@ -0,0 +1,22 @@
use crate::types::script_type_namespace::ScriptTypeNamespace;
use std::borrow::{Borrow, BorrowMut};
pub struct TypeLibrary {
script_types: Box<ScriptTypeNamespace>,
}
impl TypeLibrary {
pub fn new() -> TypeLibrary {
TypeLibrary {
script_types: Box::new(ScriptTypeNamespace::new(None)),
}
}
pub fn get_global_namespace(&self) -> &ScriptTypeNamespace {
self.script_types.borrow()
}
pub fn get_global_namespace_mut(&mut self) -> &mut ScriptTypeNamespace {
self.script_types.borrow_mut()
}
}