Skip to content

Commit

Permalink
Change: overhaul Statement
Browse files Browse the repository at this point in the history
Redefines Statement from an enum containing all information to a struct.

This Statement struct contains a start and end token as well as a
`StatementKind`. The kind contains all necessary information for
execution.

The Statement also contains information about start and end to make it
also useful when working with code manipulation e.g. transpiling.

This refactoring allows us to extend Statement information, for e.g. a
formatter, while not having to change the interpreter itself.

Overall this should give us more freedom to extend Statements.
  • Loading branch information
nichtsfrei committed Jan 11, 2024
1 parent 64a9e86 commit a88d1a1
Show file tree
Hide file tree
Showing 21 changed files with 1,269 additions and 1,278 deletions.
13 changes: 7 additions & 6 deletions rust/feed/src/oid/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use std::{fs::File, io::Read};

use nasl_interpreter::{AsBufReader, Loader};
use nasl_syntax::{IdentifierType, Statement, TokenCategory};
use nasl_syntax::{IdentifierType, Statement, StatementKind, TokenCategory};

use crate::{
update,
Expand Down Expand Up @@ -40,10 +40,11 @@ where
}

fn script_oid(stmt: &Statement) -> Option<String> {
match stmt {
Statement::Call(t, stms, _) => match t.category() {
match stmt.kind() {
StatementKind::Call(param) => match stmt.start().category() {
TokenCategory::Identifier(IdentifierType::Undefined(s)) => match s as &str {
"script_oid" => stms.first().map(|x| x.to_string()),
// maybe switch from children to patternmatching?
"script_oid" => param.children().first().map(|x| x.to_string()),
_ => None,
},
_ => None,
Expand All @@ -56,8 +57,8 @@ where
fn single(&self, key: String) -> Result<String, update::ErrorKind> {
let code = self.loader.load(key.as_ref())?;
for stmt in nasl_syntax::parse(&code) {
if let Statement::If(_, _, stmts, _, _) = stmt? {
if let Statement::Block(_, x, _) = &*stmts {
if let StatementKind::If(_, stmts, _, _) = stmt?.kind() {
if let StatementKind::Block(x) = stmts.kind() {
for stmt in x {
if let Some(oid) = Self::script_oid(stmt) {
return Ok(oid);
Expand Down
Loading

0 comments on commit a88d1a1

Please sign in to comment.