Skip to content

Commit 7e46997

Browse files
committed
chore: improve statement type generation
1 parent 3c894b8 commit 7e46997

12 files changed

+10
-22
lines changed

parser/ast.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,21 @@ impl fmt::Display for Program {
4646
}
4747

4848
#[derive(Clone, Debug, Eq, Serialize, Deserialize, Hash, PartialEq)]
49+
#[serde(untagged)]
50+
pub enum Statement {
51+
Let(Let),
52+
Return(Expression), // todo refactor to struct
53+
Expr(Expression),
54+
}
55+
56+
#[derive(Clone, Debug, Eq, Serialize, Deserialize, Hash, PartialEq)]
57+
#[serde(tag = "type")]
4958
pub struct Let {
5059
pub identifier: Token, // rust can't do precise type with enum
5160
pub expr: Expression,
5261
pub span: Span,
5362
}
5463

55-
#[derive(Clone, Debug, Eq, Serialize, Deserialize, Hash, PartialEq)]
56-
#[serde(tag = "stmt_type")]
57-
pub enum Statement {
58-
Let(Let),
59-
Return(Expression),
60-
Expr(Expression),
61-
}
62-
6364
impl fmt::Display for Statement {
6465
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
6566
match self {

parser/snapshots/parser__ast_tree_test__tests__test_array.snap

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ expression: "[1, true]"
77
"type": "Program",
88
"body": [
99
{
10-
"stmt_type": "Expr",
1110
"expr_type": "LITERAL",
1211
"type": "Array",
1312
"elements": [

parser/snapshots/parser__ast_tree_test__tests__test_binary.snap

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ expression: 1 + 2 * 3
77
"type": "Program",
88
"body": [
99
{
10-
"stmt_type": "Expr",
1110
"expr_type": "INFIX",
1211
"op": {
1312
"span": {

parser/snapshots/parser__ast_tree_test__tests__test_func_call.snap

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ expression: "add(1, 2)"
77
"type": "Program",
88
"body": [
99
{
10-
"stmt_type": "Expr",
1110
"expr_type": "FunctionCall",
1211
"callee": {
1312
"expr_type": "IDENTIFIER",

parser/snapshots/parser__ast_tree_test__tests__test_func_declaration.snap

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@ expression: "fn(x) { x };"
77
"type": "Program",
88
"body": [
99
{
10-
"stmt_type": "Expr",
1110
"expr_type": "FUNCTION",
1211
"params": [
1312
"x"
1413
],
1514
"body": [
1615
{
17-
"stmt_type": "Expr",
1816
"expr_type": "IDENTIFIER",
1917
"name": "x",
2018
"span": {

parser/snapshots/parser__ast_tree_test__tests__test_hash.snap

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ expression: "{\"a\": 1}"
77
"type": "Program",
88
"body": [
99
{
10-
"stmt_type": "Expr",
1110
"expr_type": "LITERAL",
1211
"type": "Hash",
1312
"elements": [

parser/snapshots/parser__ast_tree_test__tests__test_if.snap

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ expression: "if (x < y) { x } else { y }"
77
"type": "Program",
88
"body": [
99
{
10-
"stmt_type": "Expr",
1110
"expr_type": "IF",
1211
"condition": {
1312
"expr_type": "INFIX",
@@ -43,7 +42,6 @@ expression: "if (x < y) { x } else { y }"
4342
},
4443
"consequent": [
4544
{
46-
"stmt_type": "Expr",
4745
"expr_type": "IDENTIFIER",
4846
"name": "x",
4947
"span": {
@@ -54,7 +52,6 @@ expression: "if (x < y) { x } else { y }"
5452
],
5553
"alternate": [
5654
{
57-
"stmt_type": "Expr",
5855
"expr_type": "IDENTIFIER",
5956
"name": "y",
6057
"span": {

parser/snapshots/parser__ast_tree_test__tests__test_index.snap

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ expression: "a[1]"
77
"type": "Program",
88
"body": [
99
{
10-
"stmt_type": "Expr",
1110
"expr_type": "Index",
1211
"object": {
1312
"expr_type": "IDENTIFIER",

parser/snapshots/parser__ast_tree_test__tests__test_let.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ expression: let a = 3
77
"type": "Program",
88
"body": [
99
{
10-
"stmt_type": "Let",
10+
"type": "Let",
1111
"identifier": {
1212
"span": {
1313
"start": 4,

parser/snapshots/parser__ast_tree_test__tests__test_return.snap

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ expression: return 3
77
"type": "Program",
88
"body": [
99
{
10-
"stmt_type": "Return",
1110
"expr_type": "LITERAL",
1211
"type": "Integer",
1312
"raw": 3,

0 commit comments

Comments
 (0)