From e60a60bb5f2586813470081f465c68ed6b773168 Mon Sep 17 00:00:00 2001 From: Aleksey Yakovlev Date: Wed, 8 May 2024 14:42:02 +0700 Subject: [PATCH] added const atom support --- src/compiler/compiler.rs | 1 + src/cst/parser.rs | 15 ++++++++++++++- src/main.rs | 7 +++---- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/compiler/compiler.rs b/src/compiler/compiler.rs index a5809e3..610a329 100644 --- a/src/compiler/compiler.rs +++ b/src/compiler/compiler.rs @@ -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), diff --git a/src/cst/parser.rs b/src/cst/parser.rs index 20fed67..ad9be83 100644 --- a/src/cst/parser.rs +++ b/src/cst/parser.rs @@ -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; @@ -83,12 +84,14 @@ fn parse_emit_statement(node: &AstNode) -> Result 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 ], }) } @@ -101,6 +104,16 @@ fn parse_emit_statement(node: &AstNode) -> Result) -> 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) -> Result<(), CstParserError> { guard_node_type(node, AstNodeType::AtomHex)?; diff --git a/src/main.rs b/src/main.rs index 503b7c5..b132280 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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; @@ -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