tracer.rs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. use std::collections::HashMap;
  2. use crate::parser::{SchemaCode, SchemaWitness};
  3. use crate::state::Constants;
  4. pub struct DynamicTracer {
  5. //name: String,
  6. witness: SchemaWitness,
  7. code: SchemaCode,
  8. constants: Constants,
  9. }
  10. impl DynamicTracer {
  11. pub fn new(
  12. _name: String,
  13. witness: SchemaWitness,
  14. code: SchemaCode,
  15. constants: Constants,
  16. ) -> Self {
  17. DynamicTracer {
  18. //name,
  19. witness,
  20. code,
  21. constants,
  22. }
  23. }
  24. pub fn execute(&self) {
  25. let mut stack = HashMap::new();
  26. // Load constants
  27. for variable in self.constants.variables() {
  28. stack.insert(variable, self.constants.lookup(variable.to_string()));
  29. }
  30. // Preload stack with our witness values
  31. for (type_id, variable, _) in &self.witness {
  32. stack.insert(&variable, *type_id);
  33. }
  34. for (func_format, retvals, args, code_line) in &self.code {
  35. assert!(args.len() == func_format.param_types.len());
  36. for (variable, type_id) in args.iter().zip(func_format.param_types.iter()) {
  37. if !stack.contains_key(variable) {
  38. println!("variable '{}' is not defined", variable);
  39. panic!("{:?}", code_line);
  40. }
  41. let stack_type_id = stack.get(variable).unwrap();
  42. if stack_type_id != type_id {
  43. println!("variable '{}' has incorrect type", variable);
  44. println!("found '{:?}' but expected '{:?}'", stack_type_id, type_id);
  45. panic!("{:?}", code_line);
  46. }
  47. assert!(retvals.len() == func_format.return_type_ids.len());
  48. for (retval, ret_id) in retvals.iter().zip(func_format.return_type_ids.iter()) {
  49. // Note that later variables shadow earlier ones.
  50. // We accept this.
  51. stack.insert(retval, *ret_id);
  52. }
  53. }
  54. }
  55. }
  56. }