fix: unary reference in string; (#27)

This commit is contained in:
stefan-gorules
2023-04-26 22:26:14 +02:00
committed by GitHub
parent 16f2967ef5
commit 1e883cb810
3 changed files with 25 additions and 19 deletions
+1 -1
View File
@@ -104,7 +104,7 @@ impl<'a> DecisionTableHandler<'a> {
return None;
}
let result = self.isolate.run(rule_value.as_str());
let result = self.isolate.run_unary(rule_value.as_str());
if result.is_err() {
return None;
}
+23 -17
View File
@@ -16,6 +16,7 @@ use crate::parser::error::ParserError;
use crate::parser::{StandardParser, UnaryParser};
use crate::compiler::{Compiler, CompilerError};
use crate::lexer::token::TokenKind;
use crate::opcodes::{Opcode, Variable};
use crate::vm::{Scope, VMError, VM};
@@ -76,14 +77,6 @@ impl<'a> Isolate<'a> {
*env = ManuallyDrop::new(new_env);
}
pub fn run(&self, source: &'a str) -> Result<Value, IsolateError> {
if self.contains_ref(source) {
self.run_standard(source)
} else {
self.run_unary(source)
}
}
fn get_bump(&self) -> &'a Bump {
unsafe { std::mem::transmute::<&Bump, &'a Bump>(&self.bump.borrow()) }
}
@@ -164,6 +157,8 @@ impl<'a> Isolate<'a> {
res.try_into().map_err(|_| IsolateError::ValueCastError)
}
/// Runs unary test
/// If reference identifier is present ($) it will use standard parser
pub fn run_unary(&self, source: &'a str) -> Result<Value, IsolateError> {
self.clear();
@@ -173,14 +168,29 @@ impl<'a> Isolate<'a> {
.map_err(|source| IsolateError::LexerError { source })?;
let tkn = tokens.borrow();
let unary_disallowed = tkn
.iter()
.any(|token| token.kind == TokenKind::Identifier && token.value == "$");
let bump = self.get_bump();
let ast = match unary_disallowed {
true => {
let parser = StandardParser::try_new(tkn.as_ref(), bump)
.map_err(|source| IsolateError::ParserError { source })?;
let parser = UnaryParser::try_new(tkn.as_ref(), bump)
.map_err(|source| IsolateError::ParserError { source })?;
parser
.parse()
.map_err(|source| IsolateError::ParserError { source })?
}
false => {
let parser = UnaryParser::try_new(tkn.as_ref(), bump)
.map_err(|source| IsolateError::ParserError { source })?;
let ast = parser
.parse()
.map_err(|source| IsolateError::ParserError { source })?;
parser
.parse()
.map_err(|source| IsolateError::ParserError { source })?
}
};
let compiler = Compiler::new(ast, self.bytecode.clone(), bump);
compiler
@@ -211,8 +221,4 @@ impl<'a> Isolate<'a> {
(*self.scopes.get()).clear();
}
}
fn contains_ref(&self, source: &str) -> bool {
source.chars().any(|c| c == '$')
}
}
+1 -1
View File
@@ -537,7 +537,7 @@ fn isolate_unary_tests() {
isolate.set_reference(reference).unwrap();
for TestCase { expr, result } in cases {
assert_eq!(result, isolate.run(expr).unwrap(), "{}", expr);
assert_eq!(result, isolate.run_unary(expr).unwrap(), "{}", expr);
}
}
}