hook up compilationResult to FFI boundry
This commit is contained in:
1
rust_compiler/Cargo.lock
generated
1
rust_compiler/Cargo.lock
generated
@@ -571,6 +571,7 @@ dependencies = [
|
||||
"helpers",
|
||||
"lsp-types",
|
||||
"pretty_assertions",
|
||||
"safer-ffi",
|
||||
"thiserror",
|
||||
"tokenizer",
|
||||
]
|
||||
|
||||
@@ -3,4 +3,4 @@ mod test;
|
||||
mod v1;
|
||||
mod variable_manager;
|
||||
|
||||
pub use v1::{Compiler, CompilerConfig, Error};
|
||||
pub use v1::{CompilationResult, Compiler, CompilerConfig, Error};
|
||||
|
||||
@@ -26,7 +26,7 @@ macro_rules! compile {
|
||||
&mut writer,
|
||||
Some(crate::CompilerConfig { debug: true }),
|
||||
);
|
||||
compiler.compile()
|
||||
compiler.compile().errors
|
||||
}};
|
||||
|
||||
(debug $source:expr) => {{
|
||||
|
||||
@@ -146,13 +146,18 @@ pub struct CompilerConfig {
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct CompilationResult<'a> {
|
||||
struct CompileLocation<'a> {
|
||||
location: VariableLocation<'a>,
|
||||
/// If Some, this is the name of the temporary variable that holds the result.
|
||||
/// It must be freed by the caller when done.
|
||||
temp_name: Option<Cow<'a, str>>,
|
||||
}
|
||||
|
||||
pub struct CompilationResult<'a> {
|
||||
pub errors: Vec<Error<'a>>,
|
||||
pub source_map: HashMap<usize, Vec<Span>>,
|
||||
}
|
||||
|
||||
pub struct Compiler<'a, 'w, W: std::io::Write> {
|
||||
pub parser: ASTParser<'a>,
|
||||
function_locations: HashMap<Cow<'a, str>, usize>,
|
||||
@@ -167,7 +172,7 @@ pub struct Compiler<'a, 'w, W: std::io::Write> {
|
||||
loop_stack: Vec<(Cow<'a, str>, Cow<'a, str>)>, // Stores (start_label, end_label)
|
||||
current_return_label: Option<Cow<'a, str>>,
|
||||
/// stores (IC10 `line_num`, `Vec<Span>`)
|
||||
pub source_map: HashMap<usize, Span>,
|
||||
pub source_map: HashMap<usize, Vec<Span>>,
|
||||
/// Accumulative errors from the compilation process
|
||||
pub errors: Vec<Error<'a>>,
|
||||
}
|
||||
@@ -196,7 +201,7 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn compile(mut self) -> Vec<Error<'a>> {
|
||||
pub fn compile(mut self) -> CompilationResult<'a> {
|
||||
let expr = self.parser.parse_all();
|
||||
|
||||
// Copy errors from parser
|
||||
@@ -207,11 +212,19 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
||||
// We treat parse_all result as potentially partial
|
||||
let expr = match expr {
|
||||
Ok(Some(expr)) => expr,
|
||||
Ok(None) => return self.errors,
|
||||
Ok(None) => {
|
||||
return CompilationResult {
|
||||
source_map: self.source_map,
|
||||
errors: self.errors,
|
||||
};
|
||||
}
|
||||
Err(e) => {
|
||||
// Should be covered by parser.errors, but just in case
|
||||
self.errors.push(Error::Parse(e));
|
||||
return self.errors;
|
||||
return CompilationResult {
|
||||
errors: self.errors,
|
||||
source_map: self.source_map,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
@@ -231,7 +244,10 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
||||
|
||||
if let Err(e) = self.write_output("j main", Some(span)) {
|
||||
self.errors.push(e);
|
||||
return self.errors;
|
||||
return CompilationResult {
|
||||
errors: self.errors,
|
||||
source_map: self.source_map,
|
||||
};
|
||||
}
|
||||
|
||||
let mut scope = VariableScope::default();
|
||||
@@ -241,7 +257,10 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
||||
self.errors.push(e);
|
||||
}
|
||||
|
||||
self.errors
|
||||
CompilationResult {
|
||||
errors: self.errors,
|
||||
source_map: self.source_map,
|
||||
}
|
||||
}
|
||||
|
||||
fn write_output(
|
||||
@@ -253,7 +272,10 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
||||
self.output.write_all(b"\n")?;
|
||||
|
||||
if let Some(span) = span {
|
||||
self.source_map.insert(self.current_line, span);
|
||||
self.source_map
|
||||
.entry(self.current_line)
|
||||
.or_default()
|
||||
.push(span);
|
||||
}
|
||||
|
||||
self.current_line += 1;
|
||||
@@ -275,7 +297,7 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
||||
&mut self,
|
||||
expr: Spanned<Expression<'a>>,
|
||||
scope: &mut VariableScope<'a, '_>,
|
||||
) -> Result<Option<CompilationResult<'a>>, Error<'a>> {
|
||||
) -> Result<Option<CompileLocation<'a>>, Error<'a>> {
|
||||
match expr.node {
|
||||
Expression::Function(expr_func) => {
|
||||
self.expression_function(expr_func, scope)?;
|
||||
@@ -342,7 +364,7 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
||||
&temp_loc,
|
||||
Cow::from(format!("r{}", VariableScope::RETURN_REGISTER)),
|
||||
)?;
|
||||
Ok(Some(CompilationResult {
|
||||
Ok(Some(CompileLocation {
|
||||
location: temp_loc,
|
||||
temp_name: Some(temp_name),
|
||||
}))
|
||||
@@ -364,7 +386,7 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
||||
&loc,
|
||||
Cow::from(num.to_string()),
|
||||
)?;
|
||||
Ok(Some(CompilationResult {
|
||||
Ok(Some(CompileLocation {
|
||||
location: loc,
|
||||
temp_name: Some(temp_name),
|
||||
}))
|
||||
@@ -374,7 +396,7 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
||||
let temp_name = self.next_temp_name();
|
||||
let loc = scope.add_variable(temp_name.clone(), LocationRequest::Temp, None)?;
|
||||
self.emit_variable_assignment(temp_name.clone(), &loc, Cow::from(val))?;
|
||||
Ok(Some(CompilationResult {
|
||||
Ok(Some(CompileLocation {
|
||||
location: loc,
|
||||
temp_name: Some(temp_name),
|
||||
}))
|
||||
@@ -383,21 +405,21 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
||||
},
|
||||
Expression::Variable(name) => {
|
||||
match scope.get_location_of(&name.node, Some(name.span)) {
|
||||
Ok(loc) => Ok(Some(CompilationResult {
|
||||
Ok(loc) => Ok(Some(CompileLocation {
|
||||
location: loc,
|
||||
temp_name: None, // User variable, do not free
|
||||
})),
|
||||
Err(_) => {
|
||||
// fallback, check devices
|
||||
if let Some(device) = self.devices.get(&name.node) {
|
||||
Ok(Some(CompilationResult {
|
||||
Ok(Some(CompileLocation {
|
||||
location: VariableLocation::Device(device.clone()),
|
||||
temp_name: None,
|
||||
}))
|
||||
} else {
|
||||
self.errors
|
||||
.push(Error::UnknownIdentifier(name.node.clone(), name.span));
|
||||
Ok(Some(CompilationResult {
|
||||
Ok(Some(CompileLocation {
|
||||
location: VariableLocation::Temporary(0),
|
||||
temp_name: None,
|
||||
}))
|
||||
@@ -428,7 +450,7 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
||||
scope.free_temp(c, None)?;
|
||||
}
|
||||
|
||||
Ok(Some(CompilationResult {
|
||||
Ok(Some(CompileLocation {
|
||||
location: loc,
|
||||
temp_name: Some(result_name),
|
||||
}))
|
||||
@@ -459,7 +481,7 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
||||
scope.free_temp(name, None)?;
|
||||
}
|
||||
|
||||
Ok(Some(CompilationResult {
|
||||
Ok(Some(CompileLocation {
|
||||
location: result_loc,
|
||||
temp_name: Some(result_name),
|
||||
}))
|
||||
@@ -537,7 +559,7 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
||||
var_name: Spanned<Cow<'a, str>>,
|
||||
expr: Spanned<Expression<'a>>,
|
||||
scope: &mut VariableScope<'a, '_>,
|
||||
) -> Result<Option<CompilationResult<'a>>, Error<'a>> {
|
||||
) -> Result<Option<CompileLocation<'a>>, Error<'a>> {
|
||||
let name_str = var_name.node;
|
||||
let name_span = var_name.span;
|
||||
|
||||
@@ -553,7 +575,7 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
||||
&loc,
|
||||
Cow::from(format!("-{neg_num}")),
|
||||
)?;
|
||||
return Ok(Some(CompilationResult {
|
||||
return Ok(Some(CompileLocation {
|
||||
location: loc,
|
||||
temp_name: None,
|
||||
}));
|
||||
@@ -643,7 +665,7 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
||||
Some(name_span),
|
||||
)?;
|
||||
|
||||
if let CompilationResult {
|
||||
if let CompileLocation {
|
||||
location: VariableLocation::Constant(Literal::Number(num)),
|
||||
..
|
||||
} = result
|
||||
@@ -787,7 +809,7 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
||||
}
|
||||
};
|
||||
|
||||
Ok(Some(CompilationResult {
|
||||
Ok(Some(CompileLocation {
|
||||
location: loc,
|
||||
temp_name,
|
||||
}))
|
||||
@@ -797,7 +819,7 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
||||
&mut self,
|
||||
expr: ConstDeclarationExpression<'a>,
|
||||
scope: &mut VariableScope<'a, '_>,
|
||||
) -> Result<CompilationResult<'a>, Error<'a>> {
|
||||
) -> Result<CompileLocation<'a>, Error<'a>> {
|
||||
let ConstDeclarationExpression {
|
||||
name: const_name,
|
||||
value: const_value,
|
||||
@@ -822,7 +844,7 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
||||
LiteralOr::Literal(Spanned { node, .. }) => node,
|
||||
};
|
||||
|
||||
Ok(CompilationResult {
|
||||
Ok(CompileLocation {
|
||||
location: scope.define_const(const_name.node, value, Some(const_name.span))?,
|
||||
temp_name: None,
|
||||
})
|
||||
@@ -1280,7 +1302,7 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
||||
&mut self,
|
||||
expr: TernaryExpression<'a>,
|
||||
scope: &mut VariableScope<'a, '_>,
|
||||
) -> Result<CompilationResult<'a>, Error<'a>> {
|
||||
) -> Result<CompileLocation<'a>, Error<'a>> {
|
||||
let TernaryExpression {
|
||||
condition,
|
||||
true_value,
|
||||
@@ -1316,7 +1338,7 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
||||
if let Some(clean) = false_clean {
|
||||
scope.free_temp(clean, None)?;
|
||||
}
|
||||
Ok(CompilationResult {
|
||||
Ok(CompileLocation {
|
||||
location: result_loc,
|
||||
temp_name: Some(result_name),
|
||||
})
|
||||
@@ -1452,7 +1474,7 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
||||
&mut self,
|
||||
expr: Spanned<BinaryExpression<'a>>,
|
||||
scope: &mut VariableScope<'a, '_>,
|
||||
) -> Result<CompilationResult<'a>, Error<'a>> {
|
||||
) -> Result<CompileLocation<'a>, Error<'a>> {
|
||||
fn fold_binary_expression<'a>(expr: &BinaryExpression<'a>) -> Option<Number> {
|
||||
let (lhs, rhs) = match &expr {
|
||||
BinaryExpression::Add(l, r)
|
||||
@@ -1499,7 +1521,7 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
||||
}
|
||||
|
||||
if let Some(const_lit) = fold_binary_expression(&expr.node) {
|
||||
return Ok(CompilationResult {
|
||||
return Ok(CompileLocation {
|
||||
location: VariableLocation::Constant(Literal::Number(const_lit)),
|
||||
temp_name: None,
|
||||
});
|
||||
@@ -1545,7 +1567,7 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
||||
scope.free_temp(name, None)?;
|
||||
}
|
||||
|
||||
Ok(CompilationResult {
|
||||
Ok(CompileLocation {
|
||||
location: result_loc,
|
||||
temp_name: Some(result_name),
|
||||
})
|
||||
@@ -1555,7 +1577,7 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
||||
&mut self,
|
||||
expr: Spanned<LogicalExpression<'a>>,
|
||||
scope: &mut VariableScope<'a, '_>,
|
||||
) -> Result<CompilationResult<'a>, Error<'a>> {
|
||||
) -> Result<CompileLocation<'a>, Error<'a>> {
|
||||
match expr.node {
|
||||
LogicalExpression::Not(inner) => {
|
||||
let span = inner.span;
|
||||
@@ -1573,7 +1595,7 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
||||
scope.free_temp(name, None)?;
|
||||
}
|
||||
|
||||
Ok(CompilationResult {
|
||||
Ok(CompileLocation {
|
||||
location: result_loc,
|
||||
temp_name: Some(result_name),
|
||||
})
|
||||
@@ -1623,7 +1645,7 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
||||
scope.free_temp(name, None)?;
|
||||
}
|
||||
|
||||
Ok(CompilationResult {
|
||||
Ok(CompileLocation {
|
||||
location: result_loc,
|
||||
temp_name: Some(result_name),
|
||||
})
|
||||
@@ -1869,7 +1891,7 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
||||
expr: System<'a>,
|
||||
span: Span,
|
||||
scope: &mut VariableScope<'a, '_>,
|
||||
) -> Result<Option<CompilationResult<'a>>, Error<'a>> {
|
||||
) -> Result<Option<CompileLocation<'a>>, Error<'a>> {
|
||||
macro_rules! cleanup {
|
||||
($($to_clean:expr),*) => {
|
||||
$(
|
||||
@@ -1908,7 +1930,7 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
||||
crc_hash_signed(&str_lit),
|
||||
)));
|
||||
|
||||
Ok(Some(CompilationResult {
|
||||
Ok(Some(CompileLocation {
|
||||
location: loc,
|
||||
temp_name: None,
|
||||
}))
|
||||
@@ -2061,7 +2083,7 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
||||
Some(span),
|
||||
)?;
|
||||
|
||||
Ok(Some(CompilationResult {
|
||||
Ok(Some(CompileLocation {
|
||||
location: VariableLocation::Temporary(VariableScope::RETURN_REGISTER),
|
||||
temp_name: None,
|
||||
}))
|
||||
@@ -2091,7 +2113,7 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
||||
|
||||
cleanup!(device_hash_cleanup, logic_type_cleanup, batch_mode_cleanup);
|
||||
|
||||
Ok(Some(CompilationResult {
|
||||
Ok(Some(CompileLocation {
|
||||
location: VariableLocation::Persistant(VariableScope::RETURN_REGISTER),
|
||||
temp_name: None,
|
||||
}))
|
||||
@@ -2129,7 +2151,7 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
||||
batch_mode_cleanup
|
||||
);
|
||||
|
||||
Ok(Some(CompilationResult {
|
||||
Ok(Some(CompileLocation {
|
||||
location: VariableLocation::Persistant(VariableScope::RETURN_REGISTER),
|
||||
temp_name: None,
|
||||
}))
|
||||
@@ -2159,7 +2181,7 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
||||
|
||||
cleanup!(hash_cleanup, slot_cleanup, logic_cleanup);
|
||||
|
||||
Ok(Some(CompilationResult {
|
||||
Ok(Some(CompileLocation {
|
||||
location: VariableLocation::Persistant(VariableScope::RETURN_REGISTER),
|
||||
temp_name: None,
|
||||
}))
|
||||
@@ -2194,7 +2216,7 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
||||
expr: Math<'a>,
|
||||
span: Span,
|
||||
scope: &mut VariableScope<'a, '_>,
|
||||
) -> Result<Option<CompilationResult<'a>>, Error<'a>> {
|
||||
) -> Result<Option<CompileLocation<'a>>, Error<'a>> {
|
||||
macro_rules! cleanup {
|
||||
($($to_clean:expr),*) => {
|
||||
$(
|
||||
@@ -2213,7 +2235,7 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
||||
)?;
|
||||
|
||||
cleanup!(cleanup);
|
||||
Ok(Some(CompilationResult {
|
||||
Ok(Some(CompileLocation {
|
||||
location: VariableLocation::Persistant(VariableScope::RETURN_REGISTER),
|
||||
temp_name: None,
|
||||
}))
|
||||
@@ -2226,7 +2248,7 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
||||
)?;
|
||||
|
||||
cleanup!(cleanup);
|
||||
Ok(Some(CompilationResult {
|
||||
Ok(Some(CompileLocation {
|
||||
location: VariableLocation::Persistant(VariableScope::RETURN_REGISTER),
|
||||
temp_name: None,
|
||||
}))
|
||||
@@ -2239,7 +2261,7 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
||||
)?;
|
||||
|
||||
cleanup!(cleanup);
|
||||
Ok(Some(CompilationResult {
|
||||
Ok(Some(CompileLocation {
|
||||
location: VariableLocation::Persistant(VariableScope::RETURN_REGISTER),
|
||||
temp_name: None,
|
||||
}))
|
||||
@@ -2258,7 +2280,7 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
||||
Some(span),
|
||||
)?;
|
||||
cleanup!(var1_cleanup, var2_cleanup);
|
||||
Ok(Some(CompilationResult {
|
||||
Ok(Some(CompileLocation {
|
||||
location: VariableLocation::Persistant(VariableScope::RETURN_REGISTER),
|
||||
temp_name: None,
|
||||
}))
|
||||
@@ -2271,7 +2293,7 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
||||
)?;
|
||||
|
||||
cleanup!(cleanup);
|
||||
Ok(Some(CompilationResult {
|
||||
Ok(Some(CompileLocation {
|
||||
location: VariableLocation::Persistant(VariableScope::RETURN_REGISTER),
|
||||
temp_name: None,
|
||||
}))
|
||||
@@ -2284,7 +2306,7 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
||||
)?;
|
||||
|
||||
cleanup!(cleanup);
|
||||
Ok(Some(CompilationResult {
|
||||
Ok(Some(CompileLocation {
|
||||
location: VariableLocation::Persistant(VariableScope::RETURN_REGISTER),
|
||||
temp_name: None,
|
||||
}))
|
||||
@@ -2297,7 +2319,7 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
||||
)?;
|
||||
|
||||
cleanup!(cleanup);
|
||||
Ok(Some(CompilationResult {
|
||||
Ok(Some(CompileLocation {
|
||||
location: VariableLocation::Persistant(VariableScope::RETURN_REGISTER),
|
||||
temp_name: None,
|
||||
}))
|
||||
@@ -2310,7 +2332,7 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
||||
)?;
|
||||
|
||||
cleanup!(cleanup);
|
||||
Ok(Some(CompilationResult {
|
||||
Ok(Some(CompileLocation {
|
||||
location: VariableLocation::Persistant(VariableScope::RETURN_REGISTER),
|
||||
temp_name: None,
|
||||
}))
|
||||
@@ -2323,7 +2345,7 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
||||
)?;
|
||||
|
||||
cleanup!(cleanup);
|
||||
Ok(Some(CompilationResult {
|
||||
Ok(Some(CompileLocation {
|
||||
location: VariableLocation::Persistant(VariableScope::RETURN_REGISTER),
|
||||
temp_name: None,
|
||||
}))
|
||||
@@ -2337,7 +2359,7 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
||||
)?;
|
||||
|
||||
cleanup!(clean1, clean2);
|
||||
Ok(Some(CompilationResult {
|
||||
Ok(Some(CompileLocation {
|
||||
location: VariableLocation::Persistant(VariableScope::RETURN_REGISTER),
|
||||
temp_name: None,
|
||||
}))
|
||||
@@ -2351,7 +2373,7 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
||||
)?;
|
||||
|
||||
cleanup!(clean1, clean2);
|
||||
Ok(Some(CompilationResult {
|
||||
Ok(Some(CompileLocation {
|
||||
location: VariableLocation::Persistant(VariableScope::RETURN_REGISTER),
|
||||
temp_name: None,
|
||||
}))
|
||||
@@ -2362,7 +2384,7 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
||||
Some(span),
|
||||
)?;
|
||||
|
||||
Ok(Some(CompilationResult {
|
||||
Ok(Some(CompileLocation {
|
||||
location: VariableLocation::Persistant(VariableScope::RETURN_REGISTER),
|
||||
temp_name: None,
|
||||
}))
|
||||
@@ -2375,7 +2397,7 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
||||
)?;
|
||||
|
||||
cleanup!(clean);
|
||||
Ok(Some(CompilationResult {
|
||||
Ok(Some(CompileLocation {
|
||||
location: VariableLocation::Persistant(VariableScope::RETURN_REGISTER),
|
||||
temp_name: None,
|
||||
}))
|
||||
@@ -2388,7 +2410,7 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
||||
)?;
|
||||
|
||||
cleanup!(clean);
|
||||
Ok(Some(CompilationResult {
|
||||
Ok(Some(CompileLocation {
|
||||
location: VariableLocation::Persistant(VariableScope::RETURN_REGISTER),
|
||||
temp_name: None,
|
||||
}))
|
||||
@@ -2401,7 +2423,7 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
||||
)?;
|
||||
|
||||
cleanup!(clean);
|
||||
Ok(Some(CompilationResult {
|
||||
Ok(Some(CompileLocation {
|
||||
location: VariableLocation::Persistant(VariableScope::RETURN_REGISTER),
|
||||
temp_name: None,
|
||||
}))
|
||||
@@ -2414,7 +2436,7 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
||||
)?;
|
||||
|
||||
cleanup!(clean);
|
||||
Ok(Some(CompilationResult {
|
||||
Ok(Some(CompileLocation {
|
||||
location: VariableLocation::Persistant(VariableScope::RETURN_REGISTER),
|
||||
temp_name: None,
|
||||
}))
|
||||
|
||||
@@ -7,6 +7,7 @@ edition = "2024"
|
||||
tokenizer = { path = "../tokenizer" }
|
||||
helpers = { path = "../helpers" }
|
||||
lsp-types = { workspace = true }
|
||||
safer-ffi = { workspace = true }
|
||||
thiserror = { workspace = true }
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use super::sys_call::SysCall;
|
||||
use crate::sys_call;
|
||||
use safer_ffi::prelude::*;
|
||||
use std::{borrow::Cow, ops::Deref};
|
||||
use tokenizer::token::Number;
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use compiler::Compiler;
|
||||
use compiler::{CompilationResult, Compiler};
|
||||
use helpers::Documentation;
|
||||
use parser::{sys_call::SysCall, Parser};
|
||||
use parser::{sys_call::SysCall, tree_node::Span, Parser};
|
||||
use safer_ffi::prelude::*;
|
||||
use std::io::BufWriter;
|
||||
use tokenizer::{
|
||||
@@ -8,6 +8,20 @@ use tokenizer::{
|
||||
Tokenizer,
|
||||
};
|
||||
|
||||
#[derive_ReprC]
|
||||
#[repr(C)]
|
||||
pub struct FfiSourceMapEntry {
|
||||
pub line_number: u32,
|
||||
pub span: FfiRange,
|
||||
}
|
||||
|
||||
#[derive_ReprC]
|
||||
#[repr(C)]
|
||||
pub struct FfiCompilationResult {
|
||||
pub output_code: safer_ffi::String,
|
||||
pub source_map: safer_ffi::Vec<FfiSourceMapEntry>,
|
||||
}
|
||||
|
||||
#[derive_ReprC]
|
||||
#[repr(C)]
|
||||
pub struct FfiToken {
|
||||
@@ -34,6 +48,17 @@ pub struct FfiDocumentedItem {
|
||||
docs: safer_ffi::String,
|
||||
}
|
||||
|
||||
impl From<Span> for FfiRange {
|
||||
fn from(value: Span) -> Self {
|
||||
Self {
|
||||
start_line: value.start_line as u32,
|
||||
end_line: value.end_line as u32,
|
||||
start_col: value.start_col as u32,
|
||||
end_col: value.end_col as u32,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<lsp_types::Range> for FfiRange {
|
||||
fn from(value: lsp_types::Range) -> Self {
|
||||
Self {
|
||||
@@ -94,7 +119,7 @@ pub fn free_docs_vec(v: safer_ffi::Vec<FfiDocumentedItem>) {
|
||||
/// This should result in the ability to compile many times without triggering frame drops
|
||||
/// from the GC from a `GetBytes()` call on a string in C#.
|
||||
#[ffi_export]
|
||||
pub fn compile_from_string(input: safer_ffi::slice::Ref<'_, u16>) -> safer_ffi::String {
|
||||
pub fn compile_from_string(input: safer_ffi::slice::Ref<'_, u16>) -> FfiCompilationResult {
|
||||
let res = std::panic::catch_unwind(|| {
|
||||
let input = String::from_utf16_lossy(input.as_slice());
|
||||
let mut writer = BufWriter::new(Vec::new());
|
||||
@@ -103,19 +128,45 @@ pub fn compile_from_string(input: safer_ffi::slice::Ref<'_, u16>) -> safer_ffi::
|
||||
let parser = Parser::new(tokenizer);
|
||||
let compiler = Compiler::new(parser, &mut writer, None);
|
||||
|
||||
if !compiler.compile().is_empty() {
|
||||
return safer_ffi::String::EMPTY;
|
||||
let res = compiler.compile();
|
||||
|
||||
if !res.errors.is_empty() {
|
||||
return (safer_ffi::String::EMPTY, res.source_map);
|
||||
}
|
||||
|
||||
let Ok(compiled_vec) = writer.into_inner() else {
|
||||
return safer_ffi::String::EMPTY;
|
||||
return (safer_ffi::String::EMPTY, res.source_map);
|
||||
};
|
||||
|
||||
// Safety: I know the compiler only outputs valid utf8
|
||||
safer_ffi::String::from(unsafe { String::from_utf8_unchecked(compiled_vec) })
|
||||
(
|
||||
safer_ffi::String::from(unsafe { String::from_utf8_unchecked(compiled_vec) }),
|
||||
res.source_map,
|
||||
)
|
||||
});
|
||||
|
||||
res.unwrap_or("".into())
|
||||
if let Ok((res_str, source_map)) = res {
|
||||
FfiCompilationResult {
|
||||
source_map: source_map
|
||||
.into_iter()
|
||||
.flat_map(|(k, v)| {
|
||||
v.into_iter()
|
||||
.map(|span| FfiSourceMapEntry {
|
||||
span: span.into(),
|
||||
line_number: k as u32,
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.into(),
|
||||
output_code: res_str,
|
||||
}
|
||||
} else {
|
||||
FfiCompilationResult {
|
||||
output_code: "".into(),
|
||||
source_map: vec![].into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[ffi_export]
|
||||
@@ -184,7 +235,9 @@ pub fn diagnose_source(input: safer_ffi::slice::Ref<'_, u16>) -> safer_ffi::Vec<
|
||||
let tokenizer = Tokenizer::from(input.as_str());
|
||||
let compiler = Compiler::new(Parser::new(tokenizer), &mut writer, None);
|
||||
|
||||
let diagnosis = compiler.compile();
|
||||
let CompilationResult {
|
||||
errors: diagnosis, ..
|
||||
} = compiler.compile();
|
||||
|
||||
let mut result_vec: Vec<FfiDiagnostic> = Vec::with_capacity(diagnosis.len());
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#![allow(clippy::result_large_err)]
|
||||
|
||||
use clap::Parser;
|
||||
use compiler::Compiler;
|
||||
use compiler::{CompilationResult, Compiler};
|
||||
use parser::Parser as ASTParser;
|
||||
use std::{
|
||||
fs::File,
|
||||
@@ -90,7 +90,7 @@ fn run_logic<'a>() -> Result<(), Error<'a>> {
|
||||
|
||||
let compiler = Compiler::new(parser, &mut writer, None);
|
||||
|
||||
let errors = compiler.compile();
|
||||
let CompilationResult { errors, .. } = compiler.compile();
|
||||
|
||||
if !errors.is_empty() {
|
||||
let mut std_error = stderr();
|
||||
|
||||
Reference in New Issue
Block a user