charly-lang / charly

🐈 The Charly Programming Language | Written by @KCreate
https://charly-lang.github.io/charly/
MIT License
199 stars 10 forks source link

Refactor the way the prelude is loaded. #154

Open KCreate opened 7 years ago

KCreate commented 7 years ago

Currently the prelude is being passed via the require call and added to the new visitor.

The prelude should be the top-most layer in the hierarchy, appending program execution scopes to it. It should be passed as an initialization argument to the Visitor. This would allow us to reuse the same Visitor instance for each file that's executed.

Instead of passing the prelude to the require call, the active visitor instance would be passed.

Also, there should be an easier way of getting code executed.

Idea


source = <<-SOURCE
  let a = 25
  let b = 25
  a + b
SOURCE

vm = Charly::VM.new
result = vm.run source, "input"
puts result.value # => 50

Charly::VM should be the top-level interface to the interpreter. Config options would added to the initialize call. Charly::VM#run takes two arguments:

  1. The source code of the program
  2. An optional filename

If no filename is passed, VM-#{current_timestamp} will be used

Charly::VM should also keep track of the scope hierarchy, making sure each file is run in it's own scope, with the prelude being at the top.

The class PreludeLoader would be incorporated into the initialize call of Charly::VM and can then be removed. Configuration options would need a new class Charly::Config. This class would contain the values used for STDOUT, STDIN, STDERR, ARGV, IFLAGS, ENV, etc.

The internal methods api needs access to these config values as it contains the bindings to stdout etc.

ghost commented 7 years ago

@KCreate So this will be similar to the eval option in crystal?