Skip to content

Commit

Permalink
added const atom support
Browse files Browse the repository at this point in the history
  • Loading branch information
lexa-diky committed May 8, 2024
1 parent dfaf964 commit e60a60b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/compiler/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::ast::{AstParser, AstParserError};
use crate::compiler::{Compilation, CompilerSource, HexoCompilerContext};
use crate::cst::{CstParser, CstParserError};

#[derive(Debug)]
pub(crate) enum CompilerError {
IO(std::io::Error),
AST(AstParserError),
Expand Down
15 changes: 14 additions & 1 deletion src/cst/parser.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::path::PathBuf;
use crate::ast::{AstNode, AstNodeType, AstParserError};

use crate::ast::{AstNode, AstNodeType};
use crate::cst::{CstAtom, CstConstantStatement, CstEmitStatement, CstFunctionStatement};
use crate::cst::node::CstFile;
use crate::encoding;
Expand Down Expand Up @@ -83,12 +84,14 @@ fn parse_emit_statement(node: &AstNode) -> Result<CstEmitStatement, CstParserErr
AstNodeType::AtomHex => parse_atom_hex_into(child, &mut atoms)?,
AstNodeType::AtomUtf8 => parse_atom_utf8_into(child, &mut atoms)?,
AstNodeType::AtomBaseNumber => parse_atom_base_num_into(child, &mut atoms)?,
AstNodeType::AtomConst => parse_atom_constant_into(child, &mut atoms)?,
_ => return Err(CstParserError::UnexpectedNode {
actual: child.node_type,
expected: vec![
AstNodeType::AtomHex,
AstNodeType::AtomUtf8,
AstNodeType::AtomBaseNumber,
AstNodeType::AtomConst
],
})
}
Expand All @@ -101,6 +104,16 @@ fn parse_emit_statement(node: &AstNode) -> Result<CstEmitStatement, CstParserErr
);
}

fn parse_atom_constant_into(node: &AstNode, buf: &mut Vec<CstAtom>) -> Result<(), CstParserError> {
guard_node_type(node, AstNodeType::AtomConst)?;
let content = parse_value_of(node)?;
let atom = CstAtom::Constant { name: content };

buf.push(atom);

Ok(())
}

fn parse_atom_hex_into(node: &AstNode, buf: &mut Vec<CstAtom>) -> Result<(), CstParserError> {
guard_node_type(node, AstNodeType::AtomHex)?;

Expand Down
7 changes: 3 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::path::PathBuf;
use compiler::CompilerSource;

use crate::cli::run_build;
use crate::compiler::{HexoCompiler, HexoCompilerContext, StringCompilerSource};
use crate::compiler::{FileCompilerSource, HexoCompiler, HexoCompilerContext, StringCompilerSource};

mod cli;
mod cst_legacy;
Expand All @@ -25,12 +25,11 @@ fn new_compiler() {
let context = HexoCompilerContext::new();
let compiler = HexoCompiler::new(context);

let source = StringCompilerSource::new(
let source = FileCompilerSource::new(
PathBuf::from("sample.hexo"),
"> 01 02 03 'lol' 2x10101"
);

let compilation_result = compiler.compile(source);
let compilation_result = compiler.compile(source).unwrap();
}

// list files in directory test cases
Expand Down

0 comments on commit e60a60b

Please sign in to comment.