Skip to content

Commit 7cd0b98

Browse files
committed
Support emiting AST and Tokens
1 parent f5b13b4 commit 7cd0b98

3 files changed

Lines changed: 29 additions & 4 deletions

File tree

compiler/src/evm/mod.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@ use crate::errors::CompileError;
44
use crate::yul;
55

66
pub struct CompilerOutput {
7+
pub tokens: String,
8+
pub ast: String,
79
pub yul: String,
810
pub bytecode: String,
911
}
1012

1113
/// Compiles Fe to bytecode. It uses Yul as an intermediate representation.
1214
pub fn compile(src: &str) -> Result<CompilerOutput, CompileError> {
1315
let solc_temp = include_str!("solc_temp.json");
14-
let yul_src = yul::compile(src)?.replace("\"", "\\\"");
16+
let yul_output = yul::compile(src)?;
17+
let yul_src = yul_output.yul.replace("\"", "\\\"");
1518
let input = solc_temp.replace("{src}", &yul_src);
1619
let raw_output = solc::compile(&input);
1720
let output: serde_json::Value = serde_json::from_str(&raw_output)?;
@@ -24,8 +27,10 @@ pub fn compile(src: &str) -> Result<CompilerOutput, CompileError> {
2427
}
2528

2629
Ok(CompilerOutput {
27-
yul: yul_src,
30+
ast: yul_output.ast,
2831
bytecode,
32+
tokens: yul_output.tokens,
33+
yul: yul_src,
2934
})
3035
}
3136

compiler/src/yul/mod.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@ use crate::errors::CompileError;
55
mod mappers;
66
mod runtime;
77

8+
pub struct CompilerOutput {
9+
pub tokens: String,
10+
pub ast: String,
11+
pub yul: String,
12+
}
13+
814
/// Compiles Fe to Yul.
9-
pub fn compile(src: &str) -> Result<String, CompileError> {
15+
pub fn compile(src: &str) -> Result<CompilerOutput, CompileError> {
1016
let tokens = fe_parser::get_parse_tokens(src)?;
1117
let fe_module = fe_parser::parsers::file_input(&tokens[..])?.1.node;
1218
let context = fe_semantics::analysis(&fe_module)?;
@@ -16,7 +22,11 @@ pub fn compile(src: &str) -> Result<String, CompileError> {
1622
.values()
1723
.next()
1824
{
19-
return Ok(first_contract.to_string());
25+
return Ok(CompilerOutput {
26+
tokens: format!("{:?}", tokens),
27+
ast: format!("{:?}", fe_module),
28+
yul: first_contract.to_string(),
29+
});
2030
}
2131

2232
Err(CompileError::static_str("unable to parse tokens."))

src/main_full.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ const VERSION: &str = env!("CARGO_PKG_VERSION");
2020
arg_enum! {
2121
#[derive(PartialEq, Debug)]
2222
pub enum CompilationTarget {
23+
Ast,
2324
Bytecode,
25+
Tokens,
2426
Yul,
2527
}
2628
}
@@ -92,10 +94,18 @@ fn compile(src_file: &str, output_dir: &str, targets: Vec<CompilationTarget>) ->
9294

9395
for target in targets {
9496
match target {
97+
CompilationTarget::Ast => {
98+
let mut file_ast = fs::File::create(output_dir.join("out.ast"))?;
99+
file_ast.write_all(output.ast.as_bytes())?;
100+
}
95101
CompilationTarget::Bytecode => {
96102
let mut file_bytecode = fs::File::create(output_dir.join("out.bin"))?;
97103
file_bytecode.write_all(output.bytecode.as_bytes())?;
98104
}
105+
CompilationTarget::Tokens => {
106+
let mut file_tokens = fs::File::create(output_dir.join("out.tokens"))?;
107+
file_tokens.write_all(output.tokens.as_bytes())?;
108+
}
99109
CompilationTarget::Yul => {
100110
let mut file_yul = fs::File::create(output_dir.join("out.yul"))?;
101111
file_yul.write_all(output.yul.as_bytes())?;

0 commit comments

Comments
 (0)