aboutsummaryrefslogtreecommitdiffstats
path: root/alv/module.moon
diff options
context:
space:
mode:
authors-ol <s-ol@users.noreply.github.com>2020-08-14 09:38:30 +0000
committers-ol <s+removethis@s-ol.nu>2025-03-02 14:24:49 +0000
commit90fafe906e99311bacca56ebb444bf99ea3f7804 (patch)
tree9722a1e2cd626697caee799c873086f37ccd7bd4 /alv/module.moon
parentdocument and spec Type member fields (diff)
downloadalive-90fafe906e99311bacca56ebb444bf99ea3f7804.tar.gz
alive-90fafe906e99311bacca56ebb444bf99ea3f7804.zip
separate Module and IO code
Diffstat (limited to 'alv/module.moon')
-rw-r--r--alv/module.moon101
1 files changed, 71 insertions, 30 deletions
diff --git a/alv/module.moon b/alv/module.moon
index d530712..b10b0f9 100644
--- a/alv/module.moon
+++ b/alv/module.moon
@@ -1,41 +1,36 @@
----
-- Per-file execution context.
--
--- @classmod Module
+-- @module module
import Registry from require 'alv.registry'
import Error from require 'alv.error'
import Scope from require 'alv.scope'
import program from require 'alv.parsing'
builtins = require 'alv.builtins'
-slurp = (file) ->
- file = assert (io.open file, 'r'), Error 'io', "couldn't open '#{file}'"
- with file\read '*all'
- file\close!
-
-spit = (file, str) ->
- file = io.open file, 'w'
- file\write str
- file\close!
-
+--- Base class for Modules.
+-- @type Module
class Module
---- static functions
--- @section static
-
--- create a new Module.
-- @classmethod
- new: (@file) =>
+ new: =>
@registry = Registry!
---- members
--- @section members
+ --- check when the module source has last changed.
+ -- @function poll
+ -- @treturn number timestamp of last change
- --- check whether file was changed.
- -- @treturn bool whether the file was changed since the last call
- poll: =>
- { :mode, :modification } = (lfs.attributes @file) or {}
- assert mode == 'file', Error 'io', "not a file: '#{@file}'"
- modification
+ --- get the module basename.
+ -- @function name
+ -- @treturn string
+
+ --- get the module contents.
+ -- @function slurp
+ -- @treturn string
+
+ --- update the module contents.
+ -- @function spit
+ -- @tparam string str the updated contents
--- start an evaluation cycle.
--
@@ -43,7 +38,7 @@ class Module
-- Otherwise, register the module with the `Copilot`. Updates `root`.
eval: =>
@registry\begin_eval!
- @ast = Error.wrap "parsing '#{@file}'", -> program\match slurp @file
+ @ast = Error.wrap "parsing '#{@file}'", -> program\match @slurp!
assert @ast, Error 'syntax', "failed to parse"
scope = Scope builtins
@@ -56,17 +51,14 @@ class Module
finish: =>
tags_changed = @registry\end_eval!
if tags_changed
- spit @file, @ast\stringify!
+ @spit @ast\stringify!
--- destroy this module.
destroy: =>
@registry\destroy!
- --- get the module basename.
- -- @treturn string
- basename: => @file\match '([^/\\]+)$'
-
- __tostring: => "<Module #{@basename!}>"
+ __tostring: => "<#{@@__name} #{@name!}>"
+ __inherited: (cls) => cls.__base.__tostring = @__tostring
--- the last updated AST tree for this module.
-- @tfield ?AST ast
@@ -74,6 +66,55 @@ class Module
--- the runtime graph root of this module.
-- @tfield ?RTNode root
+--- Module type for modules loaded from the filesystem.
+-- @type FSModule
+class FSModule extends Module
+ --- create a new FSModule.
+ -- @classmethod
+ -- @tparam string file filepath
+ new: (@file) =>
+ super!
+
+ slurp: =>
+ file = assert (io.open @file, 'r'), Error 'io', "couldn't open '#{@file}'"
+ with file\read '*all'
+ file\close!
+
+ spit: (str) =>
+ file = io.open @file, 'w'
+ file\write str
+ file\close!
+
+ poll: =>
+ { :mode, :modification } = (lfs.attributes @file) or {}
+ assert mode == 'file', Error 'io', "not a file: '#{@file}'"
+ modification
+
+ name: => @file\match '([^/\\]+)$'
+
+--- Module type for modules loaded from RAM.
+-- @type StringModule
+class StringModule extends Module
+ --- create a new StringModule.
+ -- @classmethod
+ -- @tparam string name_ module name
+ -- @tparam string source module source code
+ new: (@name_, @source) =>
+ super!
+ @updated = os.time!
+
+ slurp: => @source
+
+ spit: (str) =>
+ @source = str
+ @updated = os.time!
+
+ poll: => @updated
+
+ name: => @name_
+
{
:Module
+ :FSModule
+ :StringModule
}