runBlock function

void runBlock(
  1. int start,
  2. int end,
  3. int newIndent
)

Runs some specific block of code

Implementation

void runBlock(int start, int end, int newIndent) {
  final oldIndent = indent;
  indent = newIndent;

  for (cur = start; cur < end; cur++) {
    try {
      final ln = lines[cur];
      // lprint(ln);
      if (ln['indentation'] == indent) {
        // Process line
        if (ln['type'] == LineType.Command) {
          commands[ln['command']]!(ln['arguments']);
        } else if (ln['type'] == LineType.Definition) {
          final DefinitionType? defType = ln['def_type'];
          final name = ln['name'];
          if (defType == DefinitionType.Simple) {
            // Just a simple definition
            _updateVariable(name, expEval(ln['value']));
          } else if (defType == DefinitionType.Complex) {
            // Complex definition (with math operator)
            final String? op = ln['operator'];
            var curVal = context[name];
            var newVal = expEval(ln['value']);
            // Checking operators
            if (op == '+=') {
              curVal += newVal;
            } else if (op == '-=') {
              curVal -= newVal;
            } else if (op == '*=') {
              curVal *= newVal;
            } else if (op == '/=') {
              curVal /= newVal;
            } else if (op == '%=') {
              curVal %= newVal;
            }
            _updateVariable(name, curVal);
          } else if (defType == DefinitionType.Command) {
            // Command definition
            // Calling command and putting value of it in context
            _updateVariable(name,
                commands[ln['value']['command']]!(ln['value']['arguments']));
          } else if (defType == DefinitionType.Undef) {
            // Undef
            context.remove(name);
          } else {
            // Block definition
            blocks[name] = defineBlockEnd(cur);
          }
        }
      }
    } catch (e) {
      print(error('Error while execution line ${cur + 1}. Skipping...\n$e'));
    }
  }

  indent = oldIndent;
  cur--;
}