From 5d3e8fbd6eba75837cbbf33c94874037c9d42673 Mon Sep 17 00:00:00 2001 From: Aleksey Yakovlev Date: Thu, 9 May 2024 11:37:50 +0700 Subject: [PATCH] added constant parsing to cst --- sample.hexo | 23 +---------------------- src/cst/parser.rs | 25 ++++++++++++++++++++++++- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/sample.hexo b/sample.hexo index 8b97249..7319a79 100644 --- a/sample.hexo +++ b/sample.hexo @@ -1,22 +1 @@ -$ class_name 'HelloWorld' -$ object_superclass_name 'java/lang/Object' - -# class_declaration { - > 0100 - > #len($0) - > $0 -} - -> cafe babe // Magic number - -> 0000 0034 // Java Bytecode version -> 0005 // Constant pool size -> 0700 02 // Class at index 2 - -> #class_declaration($class_name) -> 0700 04 // Class at index 4 -> #class_declaration($object_superclass_name) - -> 0021 // Supper public -> 0001 0003 // Class pointers -> 0000 0000 0000 0000 // No interfaces, fields, methods, attributes +$ hello_world 'hello world' 01 \ No newline at end of file diff --git a/src/cst/parser.rs b/src/cst/parser.rs index 2b8daa5..ea135a0 100644 --- a/src/cst/parser.rs +++ b/src/cst/parser.rs @@ -58,7 +58,7 @@ fn parse_function_body(node: &AstNode) -> Result {} + AstNodeType::StatementConst => constants.push(parse_constant(child)?), AstNodeType::StatementEmit => emits.push(parse_emit_statement(child)?), AstNodeType::StatementFn => functions.push(parse_function(child)?), _ => return Err(CstParserError::UnexpectedNode { @@ -75,6 +75,29 @@ fn parse_function_body(node: &AstNode) -> Result Result { + guard_node_type(node, AstNodeType::StatementConst)?; + let mut atom_buff = Vec::new(); + let mut name = None; + + for child in &node.children { + match child.node_type { + AstNodeType::StatementConstName => { + name = Some(parse_value_of(child)?); + } + _ => parse_atom_into(child, &mut atom_buff)?, + } + } + + Ok( + CstConstantStatement { + name: name.ok_or(CstParserError::MissingContent { node_type: AstNodeType::StatementConstName })?.to_string(), + atoms: atom_buff, + } + + ) +} + fn parse_function(node: &AstNode) -> Result { let mut emits = None; let mut functions = None;