NAC_Blockchain/cnnl-compiler/src/codegen/mod.rs

57 lines
1.4 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//! NVM字节码生成模块
//!
//! 将CNNL条款编译为NVM宪法指令0xE0-0xEF
/// NVM宪法指令操作码
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ConstitutionalOpcode {
ClauseLoad = 0xE0, // 加载宪法条款
ClauseVerify = 0xE1, // 验证宪法条款
ParamGet = 0xE2, // 获取宪法参数
PredicateEval = 0xE3, // 执行宪法谓词
ObligationCheck = 0xE4, // 检查义务履行
ReceiptIssue = 0xE5, // 签发宪政收据
AmendmentPropose = 0xE6,// 提出修正案
AmendmentVote = 0xE7, // 投票修正案
}
pub mod bytecode_generator;
pub use bytecode_generator::BytecodeGenerator;
/// 字节码生成器
pub struct CodeGenerator {
bytecode_gen: BytecodeGenerator,
}
impl CodeGenerator {
pub fn new() -> Self {
Self {
bytecode_gen: BytecodeGenerator::new(),
}
}
pub fn generate(&mut self, ast: &crate::parser::Program) -> Result<Vec<u8>, String> {
self.bytecode_gen.generate_program(ast)
}
}
impl Default for CodeGenerator {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_opcode_values() {
assert_eq!(ConstitutionalOpcode::ClauseLoad as u8, 0xE0);
assert_eq!(ConstitutionalOpcode::ClauseVerify as u8, 0xE1);
assert_eq!(ConstitutionalOpcode::ParamGet as u8, 0xE2);
}
}