Skip to content

Commit

Permalink
Merge up
Browse files Browse the repository at this point in the history
  • Loading branch information
prozacchiwawa committed Jul 19, 2024
2 parents 0f9451e + 39f1a75 commit 9ed1c3b
Show file tree
Hide file tree
Showing 10 changed files with 388 additions and 16 deletions.
49 changes: 49 additions & 0 deletions resources/tests/cldb_tree/hex.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
[
{
"Compute": [
{
"Function-Args": {},
"Function-Name": "test_with_hex.clsp",
"Output": {
"Arguments": "(() ())",
"Operator": "4",
"Operator-Location": "test_with_hex.clsp(1):10-test_with_hex.clsp(1):16",
"Result-Location": "test_with_hex.clsp(1):10-test_with_hex.clsp(1):16",
"Function": "concat",
"Row": "0",
"Value": "(())"
}
}
]
},
{
"Compute": [
{
"Function-Args": {},
"Function-Name": "test_with_hex.clsp",
"Output": {
"Arguments": "(1 0x42e576f7)",
"Function-Context": "()",
"Function": "concat",
"Operator": "14",
"Operator-Location": "test_with_hex.clsp(1):17",
"Result-Location": "test_with_hex.clsp(1):10-test_with_hex.clsp(1):16",
"Row": "1",
"Value": "0x0142e576f7"
}
}
]
},
{
"Compute": [
{
"Function-Args": {},
"Function-Name": "test_with_hex.clsp",
"Output": {
"Final": "0x0142e576f7",
"Final-Location": "test_with_hex.clsp(1):10-test_with_hex.clsp(1):16"
}
}
]
}
]
49 changes: 49 additions & 0 deletions resources/tests/cldb_tree/pre_hex.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
[
{
"Compute": [
{
"Function-Args": {},
"Function-Name": "test_with_hex.clsp",
"Output": {
"Arguments": "(() ())",
"Operator": "4",
"Operator-Location": "test_with_hex.clsp(1):10-test_with_hex.clsp(1):16",
"Result-Location": "test_with_hex.clsp(1):10-test_with_hex.clsp(1):16",
"Function": "concat",
"Row": "0",
"Value": "(())"
}
}
]
},
{
"Compute": [
{
"Function-Args": {},
"Function-Name": "test_with_hex.clsp",
"Output": {
"Arguments": "(1 1122334455)",
"Function-Context": "()",
"Function": "concat",
"Operator": "14",
"Operator-Location": "test_with_hex.clsp(1):17",
"Result-Location": "test_with_hex.clsp(1):10-test_with_hex.clsp(1):16",
"Row": "1",
"Value": "5417301751"
}
}
]
},
{
"Compute": [
{
"Function-Args": {},
"Function-Name": "test_with_hex.clsp",
"Output": {
"Final": "5417301751",
"Final-Location": "test_with_hex.clsp(1):10-test_with_hex.clsp(1):16"
}
}
]
}
]
20 changes: 20 additions & 0 deletions resources/tests/test_string_repr.clsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
;;
;; This program is used by the test_quote_string_generation test
;; which checks the representation of these outputs and ensures
;; that they're properly accepted by brun, which uses the classic
;; chialisp tokenizer/parser.
;;
;; the qs macro appends the given string onto "test"
;; the atom macro gives a quoted atom starting with test and
;; ending with the text given in the string.
;;
;; modern's macro system makes these different objects.
;;
(mod ()
(include *standard-cl-23*)

(defmac qs (X) (string-append "test" X))
(defmac atom (X) (c 1 (string->symbol (string-append "test" X))))

(list (qs '"') (qs "'") (qs " hi") (atom '"') (atom "'") (atom "_hi"))
)
27 changes: 24 additions & 3 deletions src/classic/clvm_tools/cmds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ use crate::classic::platform::argparse::{
TArgOptionAction, TArgumentParserProps,
};

use crate::compiler::cldb::{hex_to_modern_sexp, CldbNoOverride, CldbRun, CldbRunEnv};
use crate::compiler::cldb::{
hex_to_modern_sexp, improve_presentation, CldbNoOverride, CldbRun, CldbRunEnv, FAVOR_HEX,
};
use crate::compiler::cldb_hierarchy::{HierarchialRunner, HierarchialStepResult, RunPurpose};
use crate::compiler::clvm::start_step;
use crate::compiler::compiler::DefaultCompilerOpts;
Expand Down Expand Up @@ -403,6 +405,8 @@ pub fn cldb_hierarchy(args: CldbHierarchyArgs) -> Vec<BTreeMap<String, YamlEleme
args.args,
);

runner.set_flags(args.flags);

let mut output_stack = vec![Vec::new()];

loop {
Expand Down Expand Up @@ -435,7 +439,13 @@ pub fn cldb_hierarchy(args: CldbHierarchyArgs) -> Vec<BTreeMap<String, YamlEleme
);
let mut arg_values = BTreeMap::new();
for (k, v) in runner.running[run_idx].named_args.iter() {
arg_values.insert(k.clone(), YamlElement::String(format!("{}", v.clone())));
arg_values.insert(
k.clone(),
YamlElement::String(format!(
"{}",
improve_presentation(v.clone(), args.flags)
)),
);
}
function_entry.insert(
"Function-Args".to_string(),
Expand Down Expand Up @@ -534,6 +544,12 @@ pub fn cldb(args: &[String]) {
.set_type(Rc::new(PathOrCodeConv {}))
.set_help("path to symbol file".to_string()),
);
parser.add_argument(
vec!["-X".to_string(), "--favor-hex".to_string()],
Argument::new()
.set_action(TArgOptionAction::StoreTrue)
.set_help("favor hex output to integer".to_string()),
);
parser.add_argument(
vec!["-p".to_string(), "--only-print".to_string()],
Argument::new()
Expand Down Expand Up @@ -606,6 +622,7 @@ pub fn cldb(args: &[String]) {
};

let only_print = parsed_args.get("only_print").map(|_| true).unwrap_or(false);
let favor_hex = parsed_args.get("favor_hex").map(|_| true).unwrap_or(false);

let runner = Rc::new(DefaultProgramRunner::new());

Expand Down Expand Up @@ -699,7 +716,7 @@ pub fn cldb(args: &[String]) {
symbol_table: Rc::new(use_symbol_table),
prog: program,
args: env,
flags: 0,
flags: if favor_hex { FAVOR_HEX } else { 0 },
});

// Print the tree
Expand All @@ -710,6 +727,10 @@ pub fn cldb(args: &[String]) {

let step = start_step(program, env);
let mut cldbrun = CldbRun::new(runner, Rc::new(prim_map), Box::new(cldbenv), step);
if favor_hex {
cldbrun.set_flags(FAVOR_HEX);
}

let print_tree = |output: &mut Vec<_>, result: &BTreeMap<String, String>| {
let mut cvt_subtree = BTreeMap::new();
for (k, v) in result.iter() {
Expand Down
59 changes: 53 additions & 6 deletions src/compiler/cldb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ use crate::compiler::srcloc::Srcloc;
use crate::util::u8_from_number;
use crate::util::Number;

pub const FAVOR_HEX: u32 = 1;

fn print_atom() -> SExp {
SExp::Atom(Srcloc::start("*print*"), b"$print$".to_vec())
}
Expand Down Expand Up @@ -109,6 +111,7 @@ pub struct CldbRun {
in_expr: bool,
row: usize,
print_only: bool,
flags: u32,

outputs_to_step: HashMap<Number, PriorResult>,
}
Expand All @@ -133,6 +136,37 @@ fn humanize(a: Rc<SExp>) -> Rc<SExp> {
}
}

pub fn improve_presentation(a: Rc<SExp>, flags: u32) -> Rc<SExp> {
match a.borrow() {
SExp::Atom(l, av) => {
if av.len() > 2 && (flags & FAVOR_HEX) != 0 {
Rc::new(SExp::QuotedString(l.clone(), b'x', av.clone()))
} else {
a.clone()
}
}
SExp::Integer(l, i) => {
// If it has a nice string representation then show that.
let bytes_of_int = u8_from_number(i.clone());
if bytes_of_int.len() > 2 && (flags & FAVOR_HEX) != 0 {
Rc::new(SExp::QuotedString(l.clone(), b'x', bytes_of_int))
} else {
a.clone()
}
}
SExp::Cons(loc, l, r) => {
let new_l = improve_presentation(l.clone(), flags);
let new_r = improve_presentation(r.clone(), flags);
if Rc::as_ptr(&new_l) == Rc::as_ptr(l) && Rc::as_ptr(&new_r) == Rc::as_ptr(r) {
return a.clone();
}

Rc::new(SExp::Cons(loc.clone(), new_l, new_r))
}
_ => a.clone(),
}
}

fn is_print_request(a: &SExp) -> Option<(Srcloc, Rc<SExp>)> {
if let SExp::Cons(l, f, r) = a {
if &print_atom() == f.borrow() {
Expand Down Expand Up @@ -166,9 +200,14 @@ impl CldbRun {
row: 0,
outputs_to_step: HashMap::<Number, PriorResult>::new(),
print_only: false,
flags: 0,
}
}

pub fn set_flags(&mut self, flags: u32) {
self.flags = flags;
}

pub fn set_print_only(&mut self, pronly: bool) {
self.print_only = pronly;
}
Expand Down Expand Up @@ -207,7 +246,10 @@ impl CldbRun {
if self.should_print_basic_output() {
self.to_print
.insert("Result-Location".to_string(), l.to_string());
self.to_print.insert("Value".to_string(), x.to_string());
self.to_print.insert(
"Value".to_string(),
improve_presentation(x.clone(), self.flags).to_string(),
);
self.to_print
.insert("Row".to_string(), self.row.to_string());

Expand All @@ -228,12 +270,14 @@ impl CldbRun {
}
}
Ok(RunStep::Done(l, x)) => {
let final_value = improve_presentation(x.clone(), self.flags);
self.to_print
.insert("Final-Location".to_string(), l.to_string());
self.to_print.insert("Final".to_string(), x.to_string());
self.to_print
.insert("Final".to_string(), final_value.to_string());

self.ended = true;
self.final_result = Some(x.clone());
self.final_result = Some(final_value);
swap(&mut self.to_print, &mut result);
produce_result = true;
}
Expand Down Expand Up @@ -269,8 +313,8 @@ impl CldbRun {
if should_print_basic_output {
self.env.add_context(
sexp.borrow(),
c.borrow(),
Some(a.clone()),
improve_presentation(c.clone(), self.flags).borrow(),
Some(improve_presentation(a.clone(), self.flags)),
&mut self.to_print,
);
self.env.add_function(sexp, &mut self.to_print);
Expand All @@ -281,7 +325,10 @@ impl CldbRun {
Err(RunFailure::RunExn(l, s)) => {
self.to_print
.insert("Throw-Location".to_string(), l.to_string());
self.to_print.insert("Throw".to_string(), s.to_string());
self.to_print.insert(
"Throw".to_string(),
improve_presentation(s.clone(), self.flags).to_string(),
);

swap(&mut self.to_print, &mut result);
self.ended = true;
Expand Down
19 changes: 17 additions & 2 deletions src/compiler/cldb_hierarchy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pub struct HierarchialRunner {
input_file: Option<String>,
program_lines: Rc<Vec<String>>,
prog: Rc<SExp>,
flags: u32,

pub running: Vec<HierarchyFrame>,
}
Expand Down Expand Up @@ -233,6 +234,7 @@ impl HierarchialRunner {
program_lines,
error: false,
prog: prog.clone(),
flags: 0,

running: vec![HierarchyFrame {
purpose: RunPurpose::Main,
Expand All @@ -258,6 +260,13 @@ impl HierarchialRunner {
}
}

pub fn set_flags(&mut self, flags: u32) {
self.flags = flags;
for r in self.running.iter_mut() {
r.run.set_flags(flags);
}
}

pub fn is_ended(&self) -> bool {
self.running.is_empty()
|| self.error
Expand All @@ -267,7 +276,7 @@ impl HierarchialRunner {
fn push_synthetic_stack_frame(&mut self, current_env: Rc<SExp>, info: &RunStepRelevantInfo) {
let arg_step = clvm::start_step(info.prog.clone(), info.runtime_argument_values.clone());

let arg_run = CldbRun::new(
let mut arg_run = CldbRun::new(
self.runner.clone(),
self.prim_map.clone(),
Box::new(CldbRunEnv::new(
Expand All @@ -278,6 +287,8 @@ impl HierarchialRunner {
arg_step,
);

arg_run.set_flags(self.flags);

let mut named_args = HashMap::new();
get_args_from_env(
&mut named_args,
Expand Down Expand Up @@ -306,7 +317,7 @@ impl HierarchialRunner {

// Make an empty frame to repopulate (maybe option here?).
let step = clvm::start_step(info.prog.clone(), current_env.clone());
let run = CldbRun::new(
let mut run = CldbRun::new(
self.runner.clone(),
self.prim_map.clone(),
Box::new(CldbRunEnv::new(
Expand All @@ -317,6 +328,8 @@ impl HierarchialRunner {
step,
);

run.set_flags(self.flags);

self.running.push(HierarchyFrame {
purpose: RunPurpose::Main,

Expand Down Expand Up @@ -380,6 +393,8 @@ impl HierarchialRunner {
step,
);

self.running[idx].run.set_flags(self.flags);

Ok(HierarchialStepResult::ShapeChange)
} else if let Some(info) = relevant_run_step_info(&self.symbol_table, &current_step) {
// Create a frame based on the last argument.
Expand Down
Loading

0 comments on commit 9ed1c3b

Please sign in to comment.