user3050324
user3050324

Reputation: 41

Learning DLR (how to implement a language on top of it)

I am trying to learn how to write a simple scripting language on top of DLR, by playing with a very old DLR example called ToyScript. However ToyScript does not seem to support the following structure of a script, which I would like to use in my implementation :

print b()
def b() { 
  return 1 
}

It raises an exception, exactly as in most statically compiled languages.'

If the script follows a "static languages paradigm" :

def b() { 
  return 1 
}
print b()

ToyScript works without problems.

My question is : how the former should be done in DLR ?

[Obviously I am looking for a description of a solution, and not for a solution itself :)]

Upvotes: 4

Views: 605

Answers (1)

Li Chen
Li Chen

Reputation: 171

There are a few possible implementations. The first is to require an execution to create a function. With this way, you cannot invoke a function before a function is created with an execution. The second way is to create the all the functions when you parse the code and execute the global scripts. With this way, function declaration can appear anywhere in the code because the functions are already created before any execution. The draw back is that you need to create all the functions no matter you invoke them or not. Then there is an in-between way; when you parse the code for the first time, you store the abstract syntax tree (AST) of the functions in a function table. Then when you want to invoke a function, look for the function declaration in the function table and then compile or interpret from the AST. Compare the following two JavaScript snippets and you will have a good idea.

console.log(b());
function b() {
    return 1;
}

and

console.log(b());
var b = function() {
    return 1;
}

Upvotes: 1

Related Questions