The MAXScript engine executes MAXScript code using the following steps:
Parser
object (found in maxscrpt
\parser.h) is instantiated with an output stream (i.e. a CharStream
object found maxscrpt
\streams.h) for debugging and error output.Parser::Compile
(CharStream*) method is called, which will return an expression that can be executed represented as a Value
*. This might be CodeTree
instance, but could also be a simple literal.Value::eval()
is then called to the CodeTree
instance or do nothing if the Value
is a literal..The following code snippet demonstrates these steps:
Value* file_in(CharStream* source, int quiet)
{
// declare a thread-safe local variable called vl with three fields
three_typed_value_locals(Parser* parser, Value* code, Value* result);
CharStream* out = thread_local(current_stdout);
vl.parser = new Parser (out);
if (!quiet)
source->log_to(out);
// loop through file compiling and evaluating all expressions
try
{
source->flush_whitespace();
while (!source->at_eos())
{
vl.code = vl.parser->compile(source);
vl.result = vl.code->eval();
if (!quiet)
vl.result->print();
source->flush_whitespace();
}
source->close();
}
catch (...)
{
// catch any errors and tell what file we are in if any
out->puts("Error occurred during fileIn: ");
source->sprin1(out);
out->puts("\n");
throw;
}
//return last expression in stream as result
if (vl.result == NULL)
vl.result = &ok;
return_value(vl.result);
}