diff options
| author | s-ol <s-ol@users.noreply.github.com> | 2020-02-10 21:18:49 +0000 |
|---|---|---|
| committer | s-ol <s-ol@users.noreply.github.com> | 2020-02-10 21:18:49 +0000 |
| commit | 869547c55d6b082f34cca274bf0b978d5a7bdba8 (patch) | |
| tree | 5ec5e9183d90e2f953b79b456546406997db269a /core | |
| parent | add doc, import, import*, defn (diff) | |
| download | alive-869547c55d6b082f34cca274bf0b978d5a7bdba8.tar.gz alive-869547c55d6b082f34cca274bf0b978d5a7bdba8.zip | |
envelopes
Diffstat (limited to 'core')
| -rw-r--r-- | core/const.moon | 6 | ||||
| -rw-r--r-- | core/init.moon | 20 | ||||
| -rw-r--r-- | core/parsing.moon | 56 | ||||
| -rw-r--r-- | core/scope.moon | 3 |
4 files changed, 83 insertions, 2 deletions
diff --git a/core/const.moon b/core/const.moon index b356f49..45c44eb 100644 --- a/core/const.moon +++ b/core/const.moon @@ -15,6 +15,7 @@ class Const sym: true str: true num: true + bool: true scope: true op: true opdef: true @@ -53,7 +54,9 @@ class Const stringify: => @raw - clone: (prefix) => Const @type, @value, @raw + clone: (prefix) => @ + -- in case of doubt: + -- clone: (prefix) => Const @type, @value, @raw -- static __tostring: => @@ -74,6 +77,7 @@ class Const @num: (num) -> Const 'num', num, tostring num @str: (str) -> Const 'str', str, "'#{str}'" @sym: (sym) -> Const 'sym', sym, sym + @bool: (bool) -> Const 'bool', bool, tostring bool @empty: -> Const 'str', '', "''" @wrap: (val, name='(unknown)') -> diff --git a/core/init.moon b/core/init.moon index f0456c2..c91f0e4 100644 --- a/core/init.moon +++ b/core/init.moon @@ -7,9 +7,29 @@ import Scope from require 'core.scope' load_! import Cell, RootCell from require 'core.cell' +import cell, program from require 'core.parsing' { :Const, :Cell, :RootCell :Op, :Action, :FnDef :Scope + + parse: program\match + eval: do + class BuiltinRegistry + new: => + @last = 1 + + register: (thing, tag) => + with tag or Const.sym "builtin.#{@last}" + @last += 1 + + registry = BuiltinRegistry! + + (str, inject) -> + scope = Scope.from_table require 'lib.builtin' + scope\use inject if inject + + ast = assert (cell\match str), "failed to parse: #{str}" + Const.wrap ast\eval scope, registry } diff --git a/core/parsing.moon b/core/parsing.moon new file mode 100644 index 0000000..5186cb8 --- /dev/null +++ b/core/parsing.moon @@ -0,0 +1,56 @@ +import Const from require 'core.const' +import Cell, RootCell from require 'core.cell' +import R, S, P, V, C, Ct from require 'lpeg' + +-- whitespace +wc = S ' \t\r\n' +comment = P { + 'comment', + expr: (P '(') * ((V 'expr') + (1 - P ')'))^0 * (P ')') + comment: (P '#(') * ((V 'expr') + (1 - P ')'))^0 * (P ')') +} +space = (wc^1 * (comment * wc^1)^0) / 1 -- required whitespace +mspace = (comment + wc)^0 / 1 -- optional whitespace + +-- atoms +sym = ((R 'az', 'AZ') + (S '-_+*/.!?')) ^ 1 / Const\parse 'sym' + +strd = '"' * (C ((P '\\"') + (P '\\\\') + (1 - P '"'))^0) * '"' / Const\parse 'str', '\"' +strq = "'" * (C ((P "\\'") + (P '\\\\') + (1 - P "'"))^0) * "'" / Const\parse 'str', '\'' +str = strd + strq + +digit = R '09' +int = digit^1 +float = (digit^1 * '.' * digit^0) + (digit^0 * '.' * digit^1) +num = (float + int) / Const\parse 'num' + +atom = num + sym + str + +expr = (V 'cell') + atom +explist = Ct mspace * (V 'expr') * (space * (V 'expr'))^0 * mspace + +tag = (P '[') * atom * (P ']') +cell = (P '(') * tag^-1 * (V 'explist') * (P ')') / Cell\parse + +root = P { + (V 'explist') / RootCell\parse + :expr, :explist, :cell +} + +cell = P { + 'cell' + :expr, :explist, :cell +} + +program = root * -1 + +{ + :comment + :space + :atom + :expr + :explist + :cell + :root + :program +} diff --git a/core/scope.moon b/core/scope.moon index a840ba7..ed4d67d 100644 --- a/core/scope.moon +++ b/core/scope.moon @@ -15,7 +15,7 @@ class Scope L\trace "found #{val} in #{@}" return val - start, rest = key\match '^(.-)/(.*)' + start, rest = key\match '^(.-)/(.+)' if not start return @parent and L\push -> @parent\get key @@ -25,6 +25,7 @@ class Scope scope\getc!\get rest, "#{prefix}#{start}/" use: (other) => + L\trace "using defs from #{other} in #{@}" for k, v in pairs other.values @values[k] = v |
