wip
This commit is contained in:
@@ -271,10 +271,17 @@ pub enum BaseValue {
|
||||
Nil,
|
||||
Function(LoxFunction),
|
||||
NativeFunction(NativeFunction),
|
||||
Return {
|
||||
value: Box<BaseValue>,
|
||||
labels: Vec<String>,
|
||||
},
|
||||
}
|
||||
|
||||
struct ReturnValue {
|
||||
label: Vec<String>,
|
||||
value: BaseValue,
|
||||
}
|
||||
|
||||
impl ReturnValue {
|
||||
pub fn new(label: Vec<String>, value: BaseValue) -> Self {
|
||||
Self { label, value }
|
||||
}
|
||||
}
|
||||
|
||||
impl BaseValue {
|
||||
@@ -296,9 +303,6 @@ impl Display for BaseValue {
|
||||
BaseValue::Nil => write!(f, "nil"),
|
||||
BaseValue::Function(..) => write!(f, "<fn lox function>"),
|
||||
BaseValue::NativeFunction(..) => write!(f, "<native native function>"),
|
||||
BaseValue::Return { labels, .. } => {
|
||||
write!(f, "<return value [return:{}]>", labels.join(":"))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
use crate::frontend::source_registry::{SourceRegistry, SourceSlice};
|
||||
use std::fmt;
|
||||
use crate::{
|
||||
common::base_value::BaseValue,
|
||||
frontend::source_registry::{SourceRegistry, SourceSlice},
|
||||
};
|
||||
use std::{error::Error, fmt};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum LoxError {
|
||||
@@ -397,7 +400,7 @@ impl fmt::Display for LoxError {
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for LoxError {}
|
||||
impl Error for LoxError {}
|
||||
|
||||
pub type LoxResult<T> = Result<T, LoxError>;
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ use std::{
|
||||
io::ErrorKind,
|
||||
};
|
||||
|
||||
use crate::common::lox_result::{io_error, LoxError, LoxResult};
|
||||
use crate::common::lox_result::{io_error, LoxResult};
|
||||
|
||||
pub struct SourceRegistry {
|
||||
pub sources: Vec<SourceFile>,
|
||||
|
||||
@@ -263,10 +263,7 @@ impl PrettyPrint for Stmt {
|
||||
let indent = ctx.indent();
|
||||
|
||||
match self {
|
||||
Stmt::Expression {
|
||||
expression,
|
||||
return_value,
|
||||
} => {
|
||||
Stmt::Expression { expression, .. } => {
|
||||
if ctx.config.compact {
|
||||
let expr_str =
|
||||
pretty_print_with_config(expression.as_ref(), &PrettyConfig::compact());
|
||||
@@ -279,10 +276,7 @@ impl PrettyPrint for Stmt {
|
||||
write!(f, "{}}}", indent)
|
||||
}
|
||||
}
|
||||
Stmt::Print {
|
||||
expression,
|
||||
return_value,
|
||||
} => {
|
||||
Stmt::Print { expression, .. } => {
|
||||
if ctx.config.compact {
|
||||
let expr_str =
|
||||
pretty_print_with_config(expression.as_ref(), &PrettyConfig::compact());
|
||||
@@ -296,9 +290,7 @@ impl PrettyPrint for Stmt {
|
||||
}
|
||||
}
|
||||
Stmt::VarDeclaration {
|
||||
name,
|
||||
initializer,
|
||||
return_value,
|
||||
name, initializer, ..
|
||||
} => {
|
||||
if ctx.config.compact {
|
||||
match initializer {
|
||||
@@ -325,11 +317,7 @@ impl PrettyPrint for Stmt {
|
||||
write!(f, "{}}}", indent)
|
||||
}
|
||||
}
|
||||
Stmt::VarAssigment {
|
||||
name,
|
||||
value,
|
||||
return_value,
|
||||
} => {
|
||||
Stmt::VarAssigment { name, value, .. } => {
|
||||
if ctx.config.compact {
|
||||
let value_str =
|
||||
pretty_print_with_config(value.as_ref(), &PrettyConfig::compact());
|
||||
@@ -343,10 +331,7 @@ impl PrettyPrint for Stmt {
|
||||
write!(f, "{}}}", indent)
|
||||
}
|
||||
}
|
||||
Stmt::Return {
|
||||
expression,
|
||||
return_value,
|
||||
} => {
|
||||
Stmt::Return { expression, .. } => {
|
||||
if ctx.config.compact {
|
||||
let expr_str =
|
||||
pretty_print_with_config(expression.as_ref(), &PrettyConfig::compact());
|
||||
@@ -360,9 +345,7 @@ impl PrettyPrint for Stmt {
|
||||
}
|
||||
}
|
||||
Stmt::Block {
|
||||
statements,
|
||||
label,
|
||||
return_value,
|
||||
statements, label, ..
|
||||
} => {
|
||||
if ctx.config.compact {
|
||||
write!(f, "{{ ... [{}] ({} statements) }}", label, statements.len())
|
||||
@@ -387,7 +370,7 @@ impl PrettyPrint for Stmt {
|
||||
then_branch,
|
||||
elif_branch,
|
||||
else_branch,
|
||||
return_value,
|
||||
..
|
||||
} => {
|
||||
if ctx.config.compact {
|
||||
let cond_str =
|
||||
@@ -438,9 +421,7 @@ impl PrettyPrint for Stmt {
|
||||
}
|
||||
}
|
||||
Stmt::While {
|
||||
condition,
|
||||
body,
|
||||
return_value,
|
||||
condition, body, ..
|
||||
} => {
|
||||
write!(f, "{}while (", ctx.child_context(true).indent())?;
|
||||
condition.pretty_print(&ctx.child_context(true), f)?;
|
||||
@@ -453,7 +434,7 @@ impl PrettyPrint for Stmt {
|
||||
condition,
|
||||
increment,
|
||||
body,
|
||||
return_value,
|
||||
..
|
||||
} => {
|
||||
write!(f, "{}for (", ctx.child_context(true).indent())?;
|
||||
variable.pretty_print(&ctx.child_context(true), f)?;
|
||||
|
||||
Reference in New Issue
Block a user