about summary refs log tree commit diff stats
path: root/subx/053new_segment.subx
Commit message (Collapse)AuthorAgeFilesLines
* 4832Kartik Agaram2018-12-041-0/+1
| | | | | | Let's start adding ':end' labels in all functions, just because it helps us visualize where function calls end in traces, thanks to the '--map' commandline argument.
* 4824Kartik Agaram2018-12-031-7/+7
|
* 4818Kartik Agaram2018-12-021-1/+1
|
* 4817Kartik Agaram2018-12-021-6/+6
|
* 4808 - clean up comments in all subx filesKartik Agaram2018-11-301-2/+2
|
* 4802Kartik Agaram2018-11-301-9/+9
| | | | | | | | | | | | | | | | Some automated commenting cleanup. Still needs more careful manual scanning. sed -i 's/^# 1-3/# . 1-3/' *.subx */*.subx sed -i 's/^# op/# . op/' *.subx */*.subx sed -i 's/# vim/# . . vim/' *.subx */*.subx sed -i 's/^ # push args/ # . . push args/' *.subx */*.subx sed -i 's/^ # discard args/ # . . discard args/' *.subx */*.subx sed -i 's/^ # call/ # . . call/' *.subx */*.subx sed -i 's/^ # prolog/ # . prolog/' *.subx */*.subx sed -i 's/^ # epilog/ # . epilog/' *.subx */*.subx sed -i 's/^ # save registers/ # . save registers/' *.subx */*.subx sed -i 's/^ # restore registers/ # . restore registers/' *.subx */*.subx sed -i 's/ operand / register /' *.subx */*.subx
* 4801Kartik Agaram2018-11-301-43/+43
| | | | Reindent all SubX code to make some room for the new comment style.
* 4780Kartik Agaram2018-11-261-1/+1
|
* 4731Kartik Agaram2018-10-281-2/+2
|
* 4668Kartik Agaram2018-10-051-4/+4
|
* 4667Kartik Agaram2018-10-051-9/+9
| | | | | Standardize on hyphens in all names. And we'll use colons for namespacing labels in functions.
* 4662Kartik Agaram2018-10-051-1/+1
|
* 4655Kartik Agaram2018-10-021-1/+1
|
* 4652 - new subx helper: create new segmentKartik Agaram2018-10-021-0/+62
or: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
//: So far the recipes we define can't run each other. Let's fix that.

:(scenario calling_recipe)
def main [
  f
]
def f [
  3:num <- add 2, 2
]
+mem: storing 4 in location 3

:(scenario return_on_fallthrough)
def main [
  f
  1:num <- copy 0
  2:num <- copy 0
  3:num <- copy 0
]
def f [
  4:num <- copy 0
  5:num <- copy 0
]
+run: f
# running f
+run: {4: "number"} <- copy {0: "literal"}
+run: {5: "number"} <- copy {0: "literal"}
# back out to main
+run: {1: "number"} <- copy {0: "literal"}
+run: {2: "number"} <- copy {0: "literal"}
+run: {3: "number"} <- copy {0: "literal"}

:(before "struct routine {")
// Everytime a recipe runs another, we interrupt it and start running the new
// recipe. When that finishes, we continue this one where we left off.
// This requires maintaining a 'stack' of interrupted recipes or 'calls'.
struct call {
  recipe_ordinal running_recipe;
  int running_step_index;
  // End call Fields
  call(recipe_ordinal r) {
    running_recipe = r;
    running_step_index = 0;
    // End call Constructor
  }
  ~call() {
    // End call Destructor
  }
};
typedef list<call> call_stack;

:(replace{} "struct routine")
struct routine {
  call_stack calls;
  // End routine Fields
  routine(recipe_ordinal r);
  bool completed() const;
  const vector<instruction>& steps() const;
};
:(code)
routine::routine(recipe_ordinal r) {
  if (Trace_stream) {
    ++Trace_stream->callstack_depth;
    trace(9999, "trace") << "new routine; incrementing callstack depth to " << Trace_stream->callstack_depth << end();
    assert(Trace_stream->callstack_depth < 9000);  // 9998-101 plus cushion
  }
  calls.push_front(call(r));
  // End routine Constructor
}

//:: now update routine's helpers

:(delete{} "int& current_step_index()")
:(delete{} "recipe_ordinal currently_running_recipe()")
:(delete{} "const string& current_recipe_name()")
:(delete{} "const recipe& current_recipe()")
:(delete{} "const instruction& current_instruction()")

:(before "End Includes")
#define current_call() Current_routine->calls.front()
#define current_step_index() current_call().running_step_index
#define currently_running_recipe() current_call().running_recipe
#define current_recipe() get(Recipe, currently_running_recipe())
#define current_recipe_name() current_recipe().name
#define to_instruction(call) get(Recipe, (call).running_recipe).steps.at((call).running_step_index)
#define current_instruction() to_instruction(current_call())

:(after "Defined Recipe Checks")
// not a primitive; check that it's present in the book of recipes
if (!contains_key(Recipe, inst.operation)) {
  raise << maybe(get(Recipe, r).name) << "undefined operation in '" << to_original_string(inst) << "'\n" << end();
  break;
}
:(replace{} "default:" following "End Primitive Recipe Implementations")
default: {
  if (contains_key(Recipe, current_instruction().operation)) {  // error already raised in Checks above
    // not a primitive; look up the book of recipes
    if (Trace_stream) {
      ++Trace_stream->callstack_depth;
      trace(9999, "trace") << "incrementing callstack depth to " << Trace_stream->callstack_depth << end();
      assert(Trace_stream->callstack_depth < 9000);  // 9998-101 plus cushion
    }
    const call& caller_frame = current_call();
    Current_routine->calls.push_front(call(to_instruction(caller_frame).operation));
    finish_call_housekeeping(to_instruction(caller_frame), ingredients);
    // not done with caller
    write_products = false;
    fall_through_to_next_instruction = false;
    // End Non-primitive Call(caller_frame)
  }
}
:(code)
void finish_call_housekeeping(const instruction& call_instruction, const vector<vector<double> >& ingredients) {
  // End Call Housekeeping
}

:(scenario calling_undefined_recipe_fails)
% Hide_errors = true;
def main [
  foo
]
+error: main: undefined operation in 'foo'

:(scenario calling_undefined_recipe_handles_missing_result)
% Hide_errors = true;
def main [
  x:num <- foo
]
+error: main: undefined operation in 'x:num <- foo'

//:: finally, we need to fix the termination conditions for the run loop

:(replace{} "bool routine::completed() const")
bool routine::completed() const {
  return calls.empty();
}

:(replace{} "const vector<instruction>& routine::steps() const")
const vector<instruction>& routine::steps() const {
  assert(!calls.empty());
  return get(Recipe, calls.front().running_recipe).steps;
}

:(after "Running One Instruction")
// when we reach the end of one call, we may reach the end of the one below
// it, and the one below that, and so on
while (current_step_index() >= SIZE(Current_routine->steps())) {
  // Falling Through End Of Recipe
  if (Trace_stream) {
    trace(9999, "trace") << "fall-through: exiting " << current_recipe_name() << "; decrementing callstack depth from " << Trace_stream->callstack_depth << end();
    --Trace_stream->callstack_depth;
    assert(Trace_stream->callstack_depth >= 0);
  }
  Current_routine->calls.pop_front();
  if (Current_routine->calls.empty()) goto stop_running_current_routine;
  // Complete Call Fallthrough
  // todo: fail if no products returned
  ++current_step_index();
}