Skip to content

Commit

Permalink
added constant parsing to cst
Browse files Browse the repository at this point in the history
  • Loading branch information
lexa-diky committed May 9, 2024
1 parent 89a666f commit 5d3e8fb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
23 changes: 1 addition & 22 deletions sample.hexo
Original file line number Diff line number Diff line change
@@ -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
25 changes: 24 additions & 1 deletion src/cst/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ fn parse_function_body(node: &AstNode) -> Result<BodyParsingResult, CstParserErr

for child in &node.children {
match child.node_type {
AstNodeType::StatementConst => {}
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 {
Expand All @@ -75,6 +75,29 @@ fn parse_function_body(node: &AstNode) -> Result<BodyParsingResult, CstParserErr
Ok((emits, functions, constants))
}

fn parse_constant(node: &AstNode) -> Result<CstConstantStatement, CstParserError> {
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<CstFunctionStatement, CstParserError> {
let mut emits = None;
let mut functions = None;
Expand Down

0 comments on commit 5d3e8fb

Please sign in to comment.