Commit Graph

52 Commits

Author SHA1 Message Date
97d836a197 Fix test with import in test function
Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2020-11-10 18:03:53 -08:00
2f99742e85 Move ThunkBranch struct impl into Thunk::Branch
ThunkBranch standalone struct was not as useful as I was expecting it
would be. Its logic is now directly stored in Thunk::Branch variant

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2020-11-10 18:01:55 -08:00
004f2b91f8 Remove old Body compiler now that List compiler is in place
AST body is compiled first to a LISP-like IR via the ListCompiler, which
in turn converts down to a thunk. The direct AST -> Thunk compiler is no
longer needed.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2020-11-10 17:58:20 -08:00
e6df67041c Add LISP-like IR between AST and thunk
This should hopefully allow us to add more interesting modifications to
the code, and have less boilerplate when implementing new function-based
features.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2020-11-10 17:55:31 -08:00
c6212f5597 Finish up old example that shows off basic expressions and funcalls
The VM now supports the various int comparison methods. An example of a
"pow" method is given to show off recursion, which means we're now
TURING COMPLETE!

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2020-11-09 16:50:16 -08:00
357aad5306 Add strinst.__bool__ method
Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2020-11-09 16:35:42 -08:00
27b2ae9182 Rename member function builtins to be named _METH instead of _FUN
This is to differentiate between methods (which have a 'self' binding)
and functions (which are free).

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2020-11-09 16:05:40 -08:00
aca0b3c4c7 Fix order of operations in the parser
Boolean operators were in the wrong order in the parser for left
association.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2020-11-09 15:55:23 -08:00
be9a659159 Add Bool builtin type, branches, and some more stuff
* obj::Bool builtin type is used for truthiness and decision-making
* Branches are compiled and seem to be working for basic integer
  comparison
* Updated version of Shredder to what is current as of writing
* CheckTruth VM instruction that will explicitly set the condition flag
* Probably some other stuff

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2020-11-06 16:57:25 -08:00
692bb521ec Run rustfmt
Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2020-10-20 16:21:50 -07:00
ceda48988d Update IntValue alias to be i128 instead of i64 to get us a lil more range
Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2020-10-20 16:20:27 -07:00
9169f5970e Add IntValue type alias for backend integer values
Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2020-10-20 13:10:53 -07:00
d449632c5b Fix operator precedence in the parser
Operator precedence is applied only when the two rules are the same.
This is now fixed.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2020-10-20 13:09:23 -07:00
480055782e Add Str.__repr__, Str.__str__, Str.__int__, Int.__repr__, and Int.__int__ builtins
Builtin functions for builtin types. This allows the "hello world"
program to actually work how it's supposed to.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2020-10-19 17:28:38 -07:00
fee1ce2a47 Minor code rearrangement
Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2020-10-19 17:00:12 -07:00
1d6b2e35bc Fix bug where global names could only be used once in the root scope
Global names previously would only load once, and then not again because
of issues with how the root scope is compiled. This is fixed.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2020-10-19 16:57:40 -07:00
f35d44cf65 Add add, sub, mul, and div functions to integers
Arithmetic among integers is supportd. Yey!

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2020-10-19 16:41:57 -07:00
57c1aa4702 Fix bug in parser where - symbol was being parsed as a + symbol in binary expressions
Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2020-10-13 16:34:18 -07:00
902da3f2f3 Add internal error handling to VM, plus function arity
* VM is able to handle basic runtime errors although there is no way to
  catch this in executing code currently
* If a function is called with the wrong number of arguments (arity) it
  will invoke a runtime error.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2020-10-13 14:07:22 -07:00
c738c52455 Add Method type for objects
* Methods are wrappers created with an owner and a function, which
  passes the owner as the first argument when the function is called.
* Fix a small bug in the VM where the pc was being set at the wrong
  time

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2020-10-08 16:10:10 -07:00
c3121a176c Add self_referring_obj macro for self-referential objects.
Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2020-10-07 18:41:45 -07:00
16063d50f8 Add a couple of builtin functions, and the Vm::call() method
* Builtin functions print and println have been added
* If a global lookup fails, the VM will attempt to look up a builtin
* Vm::call(fun, args) allows interrupting the current execution state
  and starting a new function instead. It will return the value left on
  top of the stack.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2020-10-07 17:21:01 -07:00
76d0e6723f Add "return" statement
Functions may now be exited by returning a value or no value.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2020-10-07 16:34:23 -07:00
dd7cd04b39 Finish up function call implementation, it appears to be working
Functions are downcasted to a `Fun` trait, which will construct the
appropriate stack frame.

A few other things have been shifted around that affect internal APIs
while things are still under construction.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2020-10-07 15:48:24 -07:00
0ca8dc64b2 Remove get_local function from Vm
It does not use the global/local dichotomy and it wasn't being used

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2020-10-06 15:41:13 -07:00
43c4a9c531 Remove vm::package module, add object attribute shortcut methods
* vm::package is no longer needed since the compiler now creates
  UserFunRef objects
* Reserved object attributes can be accessed using get_* methods, e.g.
  get_plus for ease of use.
* Add some implementations for operator instructions in the VM

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2020-09-28 16:48:30 -07:00
958a6caabb Update how scope rules work, and update implementation
* Currently, scopes are only allowed to look at their locals and the
  globals. Inner functions cannot refer to values in their parent
  functions. This will change eventually.
* Scope lookup is split between globals and locals. Locals are defined
  in a scope if they are explicitly assigned to.
    * i.e. `a = foo` will treat `a` as a local in the current scope if
      it appears anywhere in that scope. This does not extend to
      setattrs; `a.b = foo` will not trigger `a` into being a local var.
* `Package` objects are no longer returned from the compiler - instead,
  a user function is returned.
* Other various changes and renames

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2020-09-27 19:33:18 -07:00
4848a342f0 Add function compilation
Functions are compiled in the most naiive way right now. I want to fix
up how scope lookups are done before it becomes too much to update.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2020-09-26 18:31:23 -07:00
3976b2135a Functions are implemented and VM should be able to handle function calls
* VM Signals are used by running functions to dictate whether a function
  should return, or if it should call another function. These signals
  can be injected at any time allowing for user functions to inject
  themselves at runtime.
* obj::Method is gone since it's not being used yet.
* Obj impls must implement as_any(&self) -> &dyn Any now. This allows
  for UserFun and NativeFun to be explicitly cast (among other things,
  in the future).

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2020-09-24 15:29:44 -07:00
f0032afe12 Add function expr parsing
* Introduce new `fn` keyword
* Function example is added to examples/expr.not

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2020-09-18 16:39:06 -07:00
337be88849 Add --action option to not executable
* --action dump, parse, or run will cause the program to do the
  appropriate action.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2020-09-18 16:37:23 -07:00
cb5689c513 Add vm:📦:Package, instruction disassembly
* Code is compiled into a vm:📦:Package which contains the
  executable code, the constants, and the local name mappings.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2020-09-18 15:45:54 -07:00
321fe8e1ea Add custom debug impl for obj::int::Int
This removes the vtable and attrs members from appearing, which muddies
up what we really care about with ints

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2020-09-18 15:39:19 -07:00
0d6f68216b Rename "Local" to "Name" when referring to variables during compilation
* Variables were previously named "Local"s because they described a
  local variable - however, this is kind of a misnomer because it
  handles globals as well. This has been changed pretty much everywhere
  when appropriate (hopefully).
* Renamed obj/names.rs to obj/reserved.rs, freeing up the "names" module
  for the new Names handle
* Renamed obj/locals.rs to obj/names.rs, see above

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2020-09-18 13:57:51 -07:00
9394239b83 Skip comments when parsing
Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2020-09-17 13:31:02 -07:00
1be0fb8a04 Add implementations for parsing assign statements, forgot to do this
Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2020-09-17 13:17:53 -07:00
534812f54d Add instruction dumper
A vector of instructions and constants can now be decompiled as text on
the terminal to give an idea of what the VM is doing.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2020-09-17 13:09:37 -07:00
be6266832e Remove dbg! macro calls
Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2020-09-16 18:58:12 -07:00
e72cbe2b8c Base compilation of some kind of file to a list of instructions seems to work(!)
I'm able to compile some basic expressions into instructions using the
`not` binary. Big first step, now we need to introduce branches and
loops to the syntax.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2020-09-16 18:01:40 -07:00
61ac00ae39 Remove 'text' member from Compile struct, since we can return file positions instead
Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2020-09-16 17:36:40 -07:00
099ee5ea0d Add local variable collection to compilation
* Upon entering a new body, the local scope variables are collected.
* VM instruction PushLocal now uses a `Local` value instead of a `Sym`
  value
* Compilation of a new AST body will implicitly create a new scope

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2020-09-16 17:27:33 -07:00
582b3a4b73 Add basic blocks and implementation of flattening thunks -> basic blocks
* Basic are a more linear way of representing code. Thunks beget basic
  blocks, which beget vectors of instructions.
* Basic blocks are also being flattened into a vector of instructions
  (hopefully, no tests done yet)
* OH yeah locals can be collected too (but currently are not being
  collected in the compiler, that should come soon)

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2020-09-16 17:18:31 -07:00
ef38680fe5 Add Vm::frames(&self) function
Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2020-09-14 16:37:16 -07:00
fdbb0a1307 Add some base VM implementations
Some instructions are currently implemented. Others are not. This is
mostly just a checkpoint so I can implement lexical name definitions.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2020-09-14 16:32:58 -07:00
372e58f620 Fold runtime/ crate into this source tree
While I like the idea of having a runtime completely decoupled from the
syntax and compiler, I don't think this is that big of a project for
that to be necessary or even useful yet.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2020-09-14 14:14:21 -07:00
8e2cbb10a4 General changes across the runtime crate in support of compile module
* Compile module is able to compile bytecode (or so it seems...)
* Runtime crate has had some new stuff added to it, mostly with objects
  and vtables. Still not 100% on the object method function call story,
  but I guess it'll be tackled when we get there.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2020-09-14 14:09:29 -07:00
e2c43dc911 Add LhsExpr parsing
Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2020-09-03 18:58:22 -07:00
4d8a815ffc Remove old TODO from parser grammar
Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2020-09-03 18:49:53 -07:00
c42b63e016 Add Body AST item and visit_body in visitor
Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2020-09-03 18:47:48 -07:00
f8819279f8 Fix parser for index and call exprs, remove old test that didn't work, add visitor pattern
Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2020-09-03 18:32:22 -07:00