/
CDC-1.2b/
CDC-1.2b/src/
parent $libraries
parent $has_messages
object $root_evaluator

var $root dbref 'root_evaluator
var $root child_index 2
var $root fertile 1
var $root manager $root_evaluator
var $root owned [$root_evaluator]
var $root owners [$]
var $root writable []
var $root readable ['parameters, 'methods, 'code]
var $root inited 1
var $root_evaluator append_str "_stmt"

method eval_ctext
    arg vars, term;
    var vars;
    
    // message is the message to be evaluated.
    // times is a list of the symbols that should be processed.
    // times = [] processes all symbols.
    if (type(term) != 'list)
        throw(~type, "Term must be a list. Was " + ($data.unparse(term)));
    if ('time in dict_keys(vars)) {
        if (type(vars['time]) != 'symbol)
            throw(~type, "Time must be a symbol.");
    } else {
        vars = dict_add(vars, 'time, 'pre);
    }
    vars = dict_add(vars, 'force, 1);
    vars = dict_add(vars, 'delay, 0);
    vars = dict_add(vars, 'debug, 0);
    vars = ._eval_ctext(vars, term);
    if ('process in dict_keys(vars))
        return vars;
    else
        return $ctext_class.new(vars['result]);
.

method apply
    arg vars, list, method, [args];
    var l, output;
    
    // call method once for each element in list.
    // the element is passed as the first argument.
    output = [];
    if (args) {
        for l in (list) {
            vars = .(method)(vars, [l, @args]);
            output = [@output, vars['result]];
        }
    } else {
        for l in (list) {
            vars = .(method)(vars, l);
            output = [@output, vars['result]];
        }
    }
    if (listlen(output) > 1)
        return dict_add(vars, 'result, ['list_type, output]);
    else if (listlen(output) == 1)
        return dict_add(vars, 'result, output[1]);
    else
        return dict_add(vars, 'result, ['list_type, []]);
.

method list_type
    arg vars, term;
    var t, output, output2;
    
    output = [];
    for t in (term) {
        if (type(t[1]) == 'list)
            t = t[1];
        vars = ._eval_ctext(vars, t);
        output = [@output, vars['result]];
    }
    return dict_add(vars, 'result, ['list_type, output]);
.

method parse_command
    arg text;
    var output, ret_val, term, word, final, first_term, final_args, token;
    
    output = [];
    term = [];
    word = "";
    while (text) {
        ret_val = text.split_on_next(["\"", "{", "%", "&", "[", " ", "}"]);
        word = ret_val[1];
        token = ret_val[2];
        text = ret_val[3];
        switch (token) {
            case "{":
                output = ._handle_word(output, term);
                ret_val = .parse_command(text);
                output = [@output, ret_val[1]];
                text = ret_val[2];
            case "[":
                output = ._handle_word(output, term);
                ret_val = .parse_list(text);
                output = [@output, ret_val[1]];
                text = ret_val[2];
            case "%":
                output = ._handle_word(output, term);
                ret_val = .parse_varref(text);
                output = [@output, ret_val[1]];
                text = ret_val[2];
            case "\"":
                output = ._handle_word(output, term);
                ret_val = .parse_string(text);
                output = [@output, ret_val[1]];
                text = ret_val[2];
            case " ":
                output = ._handle_word(output, word);
            case "}":
                output = ._handle_word(output, word);
                if (type(output[1]) != 'list)
                    output = [output];
                first_term = output[1];
                if ((first_term[1]) == 'string_type) {
                    first_term = tosym((first_term[2]) + "_stmt");
                    final_args = [];
                } else {
                    first_term = 'eval_stmt;
                    final_args = [first_term];
                }
                if (listlen(output) > 1)
                    final_args = [@(final_args + sublist(output, 2))];
                return [[first_term, final_args], text];
        }
    }
.

method normalize
    arg args;
    var key, new;
    
    //turns ctext style data into Coldc data
    switch (args[1]) {
        case 'dictionary_type:
            new = #[];
            for key in (dict_keys(args[2]))
                new = dict_add(new, key, .normalize((args[2])[key]));
            return new;
        case 'list_type:
            new = [];
            for key in (args[2])
                new = [@new, .normalize(key)];
            return new;
        default:
            return args[2];
    }
.

method fix_values
    arg stuff;
    var key, new;
    
    switch (type(stuff)) {
        case 'dictionary:
            new = #[];
            for key in (dict_keys(stuff))
                new = dict_add(new, key, .fix_values(stuff[key]));
            return new;
        case 'list:
            new = [];
            for key in (stuff)
                new = [@new, .fix_values(key)];
            return ['list_type, new];
        case 'symbol:
            return ['symbol_type, stuff];
        case 'string:
            return ['string_type, stuff];
        case 'integer:
            return ['integer_type, stuff];
        case 'dbref:
            return ['dbref_type, stuff];
    }
.

method _truth
    arg value;
    var result;
    
    switch (value[1]) {
        case 'symbol_type:
            return 1;
        case 'integer_type:
            return value[2];
        case 'string_type:
            if (((value[2]) == "0") || ((value[2]) == ""))
                return 0;
            else
                return 1;
        case 'list_type:
            return listlen(value[2]);
        default:
            return ">>ERROR: Argument to condtional not a value<<";
    }
.

method parse_list
    arg text;
    var output, ret_val, term, word, token, int;
    
    output = [];
    term = [];
    word = "";
    while (text) {
        ret_val = text.split_on_next(["[", "]", "(", "\"", "%", "&", " "]);
        word = ret_val[1];
        token = ret_val[2];
        text = ret_val[3];
        switch (token) {
            case "(":
                output = ._handle_word(output, word);
                vars = .parse_command(text);
                output = [@output, vars['result]];
                text = vars['text];
            case "[":
                output = ._handle_word(output, word);
                vars = .parse_list(text);
                output = [@output, vars['result]];
                text = vars['text];
            case "%":
                output = ._handle_word(output, word);
                vars = .parse_varref(text);
                output = [@output, vars['result]];
                text = vars['result];
            case "\"":
                output = ._handle_word(output, word);
                ret_val = .parse_string(text);
                output = [@output, vars['result]];
                text = vars['result];
            case " ":
                output = ._handle_word(output, word);
            case "]":
                output = ['list_type, ._handle_word(output, word)];
                vars = dict_add(vars, 'text, text);
                return dict_add(vars, 'result, output);
        }
    }
.

method parse_string
    arg text;
    var output, word, ret_val, is_text, token;
    
    output = [];
    word = "";
    is_text = 0;
    while (text) {
        ret_val = text.split_on_next(["\"", "{", "%", "&"]);
        word = ret_val[1];
        token = ret_val[2];
        text = ret_val[3];
        switch (token) {
            case "{":
                output = ._handle_word(output, word);
                ret_val = .parse_command(text);
                output = [@output, ret_val[1]];
                text = ret_val[2];
                is_text = 1;
            case "%":
                output = ._handle_word(output, word);
                ret_val = .parse_varref(text);
                output = [@output, ret_val[1]];
                text = ret_val[2];
                is_text = 1;
            case "&":
                output = ._handle_word(output, word);
                ret_val = .parse_charref(text);
                output = [@output, ret_val[1]];
                text = ret_val[2];
                is_text = 1;
            case "\"":
                output = ._handle_word(output, word);
                if (is_text)
                    output = ['text_stmt, output];
                if (listlen(output) == 1)
                    output = output[1];
                return [output, text];
        }
    }
    return [output, text];
.

method parse_charref
    arg text;
    var pos;
    
    pos = ";" in text;
    return [['char_type, substr(text, 1, pos - 1)], substr(text, pos + 1)];
.

method parse_varref
    arg text;
    var output, ret_val, word, token;
    
    output = [];
    word = "";
    while (text) {
        ret_val = text.split_on_next(["{", "%", "&", ";"]);
        word = ret_val[1];
        token = ret_val[2];
        text = ret_val[3];
        switch (token) {
            case "{":
                output = ._handle_word(output, word);
                ret_val = .parse_command(length, text);
                output = [@output, @ret_val[1]];
                text = ret_val[2];
            case "%":
                output = ._handle_word(output, word);
                ret_val = .parse_varref(length, text);
                output = [@output, @ret_val[1]];
                text = ret_val[2];
            case "&":
                output = ._handle_word(output, word);
                ret_val = .parse_charref(length, text);
                output = [@output, @ret_val[1]];
                text = ret_val[2];
            case ";":
                output = ._handle_word(output, word);
                return [['get_stmt, output], text];
        }
    }
.