Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ io-uring = "0.7.10"
enum_dispatch = "0.3.13"
pest = "2.8.1"
pest_derive = "2.8.1"
llvm-sys = "201.0.1"
llvm-sys = "221.1.0"
docopt = "1.1.1"
signal-hook = "0.3.18"

Expand Down
2 changes: 1 addition & 1 deletion Containerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM registry.fedoraproject.org/fedora:43 AS builder
FROM registry.fedoraproject.org/fedora:44 AS builder

ARG RUST_VERSION=stable

Expand Down
15 changes: 14 additions & 1 deletion src/script/ast.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
use std::collections::HashMap;

#[derive(Debug, Clone, PartialEq)]
pub enum ConstType {
Text(String),
Int(u64),
Float(f64),
}

#[derive(Debug, Clone, PartialEq)]
pub enum Arg {
/// Null constant
Null,

/// Simple constant
Const { text: String },
Const { value: ConstType },

/// Variable available at runtime
Var { name: String },
Expand All @@ -28,6 +35,12 @@ pub enum Instruction {

/// Send a message to a server at specified address
Ping { server: Arg },

/// Listen on a specified number of endpoints from the lower boundary
Listen { lower: Arg, n: Arg },

/// Sleep for specified amount of time
Sleep { interval: Arg },
}

#[derive(Debug, Clone, PartialEq)]
Expand Down
14 changes: 10 additions & 4 deletions src/script/grammar.peg
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ COMMENT = _{"//" ~ (!NEWLINE ~ ANY)*}
ident_char = {ASCII_ALPHA | "_" | "$"}
ident = @{ident_char ~ (ASCII_DIGIT | ident_char)*}

constant = {
"\"" ~ value ~ "\""
| ASCII_DIGIT+
}
constant = { text | float | int }

text = { "\"" ~ value ~ "\"" }
int = { ASCII_DIGIT+ }
float = { ASCII_DIGIT+ ~ "." ~ ASCII_DIGIT+ }

randomPath = { "random_path" }
randomString = { "random_string" }

dynamicName = {
randomPath
| randomString
| zipf
}

dynamic = {dynamicName ~ args}
Expand All @@ -38,6 +40,8 @@ port = { "port" }
open = { "open" }
ping = { "ping" }
debug = { "debug" }
listen = { "listen" }
sleep = { "sleep" }

funcName = {
task
Expand All @@ -46,6 +50,8 @@ funcName = {
| open
| ping
| debug
| listen
| sleep
}

exp = { "exp" }
Expand Down
89 changes: 74 additions & 15 deletions src/script/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use log::trace;
use pest::{self, Parser, error::Error};
use std::collections::HashMap;

use crate::script::ast::{Arg, Dist, Instruction, MachineInstruction, Node};
use crate::script::ast::{
Arg, ConstType, Dist, Instruction, MachineInstruction, Node,
};

#[derive(Debug)]
pub enum ParseError {
Expand Down Expand Up @@ -152,9 +154,25 @@ fn build_ast_from_instr(
.map(|arg| {
let a = first_nested_pair(arg);
match a.as_rule() {
Rule::constant => Arg::Const {
text: pair_to_string(first_nested_pair(a)),
},
Rule::constant => {
let value = first_nested_pair(a);
match value.as_rule() {
Rule::text => Arg::Const {
value: ConstType::Text(pair_to_string(
first_nested_pair(value),
)),
},
Rule::int => Arg::Const {
value: ConstType::Int(pair_to_int(value)),
},
Rule::float => Arg::Const {
value: ConstType::Float(pair_to_float(value)),
},
unknown => {
panic!("Unknown constant type {unknown:?}")
}
}
}
Rule::ident => Arg::Var {
name: pair_to_string(a),
},
Expand All @@ -167,10 +185,27 @@ fn build_ast_from_instr(
let args: Vec<Arg> = args_pair
.into_inner()
.map(|arg| {
let a =
let value =
first_nested_pair(first_nested_pair(arg));
Arg::Const {
text: pair_to_string(a),
match value.as_rule() {
Rule::text => Arg::Const {
value: ConstType::Text(pair_to_string(
first_nested_pair(value),
)),
},
Rule::int => Arg::Const {
value: ConstType::Int(pair_to_int(
value,
)),
},
Rule::float => Arg::Const {
value: ConstType::Float(pair_to_float(
value,
)),
},
unknown => panic!(
"Unknown constant type {unknown:?}"
),
}
})
.collect();
Expand Down Expand Up @@ -211,6 +246,17 @@ fn build_ast_from_instr(
server: args[0].clone(),
});
}
Rule::listen => {
instr.push(Instruction::Listen {
lower: args[0].clone(),
n: args[1].clone(),
});
}
Rule::sleep => {
instr.push(Instruction::Sleep {
interval: args[0].clone(),
});
}
unknown => panic!("Unknown instruction type {unknown:?}"),
}
}
Expand Down Expand Up @@ -253,9 +299,9 @@ fn build_ast_from_dist(pair: pest::iterators::Pair<Rule>) -> Dist {
fn string_from_pair(pair: pest::iterators::Pair<Rule>) -> String {
assert!(matches!(pair.as_rule(), Rule::constant | Rule::ident));

// Extract "value" (Constants) or "name" (Identifier)
// Extract "value" (text Constants) or "name" (Identifier)
// and convert it to String
pair_to_string(first_nested_pair(pair))
pair_to_string(first_nested_pair(first_nested_pair(pair)))
}

fn string_from_argument(
Expand All @@ -276,10 +322,23 @@ fn pair_to_string(pair: pest::iterators::Pair<Rule>) -> String {
pair.as_span().as_str().to_string()
}

fn pair_to_int(pair: pest::iterators::Pair<Rule>) -> u64 {
pair.as_span().as_str().to_string().parse().unwrap()
}

fn pair_to_float(pair: pest::iterators::Pair<Rule>) -> f64 {
pair.as_span().as_str().to_string().parse().unwrap()
}

fn first_nested_pair(
pair: pest::iterators::Pair<Rule>,
) -> pest::iterators::Pair<Rule> {
pair.into_inner().next().expect("Cannot get first pair")
let mut inner = pair.clone().into_inner();
if inner.is_empty() {
pair
} else {
inner.next().expect("Cannot get first pair")
}
}

#[cfg(test)]
Expand Down Expand Up @@ -310,7 +369,7 @@ mod tests {
instructions[0],
Instruction::Open {
path: Arg::Const {
text: "/tmp/test".to_string()
value: ConstType::Text("/tmp/test".to_string())
}
}
);
Expand Down Expand Up @@ -346,7 +405,7 @@ mod tests {
path: Arg::Dynamic {
name: "random_path".to_string(),
args: vec![Arg::Const {
text: "/tmp".to_string()
value: ConstType::Text("/tmp".to_string())
}],
}
}
Expand Down Expand Up @@ -383,7 +442,7 @@ mod tests {
instructions[0],
Instruction::Debug {
text: Arg::Const {
text: "run task stub".to_string(),
value: ConstType::Text("run task stub".to_string()),
}
}
);
Expand Down Expand Up @@ -431,7 +490,7 @@ mod tests {
instructions[0],
Instruction::Debug {
text: Arg::Const {
text: "ping server".to_string(),
value: ConstType::Text("ping server".to_string()),
}
}
);
Expand All @@ -440,7 +499,7 @@ mod tests {
instructions[1],
Instruction::Ping {
server: Arg::Const {
text: "127.0.0.1:8080".to_string(),
value: ConstType::Text("127.0.0.1:8080".to_string()),
},
}
);
Expand Down
Loading
Loading