fusion-lang 0.0.1.alpha2 → 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +9 -8
  3. data/docs/lang/design.md +240 -51
  4. data/docs/lang/implementation.md +238 -0
  5. data/docs/lang/roadmap.md +20 -36
  6. data/docs/user/explanation.md +5 -10
  7. data/docs/user/how-to-guides.md +60 -15
  8. data/docs/user/reference.md +356 -142
  9. data/docs/user/tutorial.md +21 -19
  10. data/examples/double.fsn +1 -1
  11. data/examples/factorial.fsn +2 -2
  12. data/examples/fizzbuzz.fsn +1 -4
  13. data/examples/json_test.fsn +4 -0
  14. data/examples/palindrome.fsn +1 -1
  15. data/exe/fusion +10 -10
  16. data/lib/fusion/ast.rb +2 -1
  17. data/lib/fusion/cli/decoder.rb +10 -5
  18. data/lib/fusion/cli/options.rb +130 -60
  19. data/lib/fusion/cli/parser.rb +3 -3
  20. data/lib/fusion/cli/repl.rb +30 -25
  21. data/lib/fusion/cli/serializer.rb +5 -4
  22. data/lib/fusion/cli.rb +119 -48
  23. data/lib/fusion/interpreter/builtins.rb +260 -151
  24. data/lib/fusion/interpreter/env.rb +42 -12
  25. data/lib/fusion/interpreter/error_val.rb +42 -20
  26. data/lib/fusion/interpreter/thunk.rb +53 -0
  27. data/lib/fusion/interpreter.rb +239 -82
  28. data/lib/fusion/lexer.rb +69 -3
  29. data/lib/fusion/parser.rb +189 -51
  30. data/lib/fusion/version.rb +1 -1
  31. data/stdlib/all.fsn +13 -0
  32. data/stdlib/any.fsn +12 -0
  33. data/stdlib/chars.fsn +5 -0
  34. data/stdlib/compact.fsn +6 -0
  35. data/stdlib/concat.fsn +5 -0
  36. data/stdlib/falsey.fsn +6 -0
  37. data/stdlib/filter.fsn +12 -0
  38. data/stdlib/flatten.fsn +7 -0
  39. data/stdlib/gt.fsn +9 -0
  40. data/stdlib/gte.fsn +9 -0
  41. data/stdlib/lt.fsn +9 -0
  42. data/stdlib/lte.fsn +9 -0
  43. data/stdlib/map.fsn +6 -4
  44. data/stdlib/range.fsn +2 -2
  45. data/stdlib/reduce.fsn +8 -0
  46. data/stdlib/sanitize.fsn +2 -2
  47. data/stdlib/truthy.fsn +7 -0
  48. metadata +18 -4
  49. data/lib/fusion/interpreter/file_thunk.rb +0 -39
  50. data/stdlib/mapValues.fsn +0 -5
  51. data/stdlib/math/square.fsn +0 -4
@@ -9,32 +9,54 @@ module Fusion
9
9
  class ErrorVal
10
10
  attr_reader :payload
11
11
 
12
- def initialize(payload)
12
+ def initialize(payload, runtime: false)
13
13
  @payload = payload
14
- @internal = false
14
+ @runtime = runtime
15
15
  end
16
16
 
17
- # Whether this is an interpreter-produced error (vs. a user-constructed
18
- # `!expr`). Governs serialization — see docs/user/reference.md §9.3.
19
- def internal_error?
20
- @internal
17
+ # Attach the call-site `file` to a runtime error. Idempotent.
18
+ def with_call_site(file)
19
+ # Only stamp runtime-produced errors. After this check we are sure that the payload wasn't user-constructed.
20
+ return self unless @runtime
21
+ raise Unreachable, "Unexpected runtime error payload: #{@payload}" unless @payload.is_a?(Hash)
22
+ # Don't double stamp. Idempotency.
23
+ return self if @payload.key?("file")
24
+ # Only stamp certain errors.
25
+ return self unless ["builtin", "stdlib"].include?(@payload["origin"])
26
+
27
+ # Insert "file" after "origin"
28
+ reordered = {}
29
+ @payload.each do |key, value|
30
+ reordered[key] = value
31
+ reordered["file"] = file if key == "origin"
32
+ end
33
+ @payload = reordered
34
+ self
35
+ end
36
+
37
+ # Was this error runtime-produced (as opposed to user-constructed via `!expr`)?
38
+ # Runtime errors use lenient serialization (docs/user/reference.md §9.3) and
39
+ # get a call-site `file` stamped.
40
+ def runtime?
41
+ @runtime
21
42
  end
22
43
 
23
- # Build an interpreter-produced error with the standardized payload shape
44
+ # Build a runtime-produced error with the standardized payload shape
24
45
  # documented in docs/user/reference.md §6.5.
25
- def self.internal(kind:, location:, operation:, input:, message: nil)
26
- error = new(
27
- "kind" => kind,
28
- "location" => location,
29
- "operation" => operation,
30
- "input" => input,
31
- **(message ? { "message" => message } : {})
32
- )
33
-
34
- # Mark as "@internal" to activate lenient serialization.
35
- error.instance_variable_set(:@internal, true)
36
-
37
- error
46
+ def self.from_runtime(kind:, origin:, operation:, input:, file: nil, expected: nil, message: nil)
47
+ raise Unreachable, "an error with `expected` must not also carry a `message`" if expected && message
48
+
49
+ received_error = input.is_a?(ErrorVal)
50
+
51
+ payload = { "kind" => kind, "origin" => origin }
52
+ payload["file"] = file if file
53
+ payload["operation"] = operation
54
+ payload["status"] = received_error ? 1 : 0
55
+ payload["input"] = received_error ? input.payload : input
56
+ payload["expected"] = expected if expected
57
+ payload["message"] = message if message
58
+
59
+ new(payload, runtime: true)
38
60
  end
39
61
 
40
62
  def inspect
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ # === Interpreter internals ===
4
+ #
5
+ # Lazy, memoized value of a top-level unit (a file, or an inline/REPL entry).
6
+
7
+ module Fusion
8
+ class Interpreter
9
+ class Thunk
10
+ # We use a custom Ruby error to transmit read failures between `Interpreter.evaluate_file`
11
+ # (which runs in the @compute block) and the Thunk to enforce their connection.
12
+ # If `Interpreter.evaluate_file` were to be used outside of a Thunk, the Ruby error would
13
+ # bubble and trigger an `internal_error` later on.
14
+ class ReadFailure < StandardError; end
15
+
16
+ def initialize(&compute)
17
+ @compute = compute
18
+ @state = :unforced # :unforced | :forcing | :done
19
+ @value = nil # memoized result: runtime value/error | ReadFailure
20
+ end
21
+
22
+ # `operation`/`input`/`site` describe the @-reference forcing this thunk.
23
+ # They are NOT passed to `@compute`, because they differ when evaluating the same
24
+ # Thunk for different @-references. They MUST NOT become part or the memoized value.
25
+ def force(operation: "loading code", input: NULL, site: { origin: "code", file: nil })
26
+ result = case @state
27
+ when :done
28
+ @value
29
+ when :forcing
30
+ # Re-entering while still computing results in a non-productive data cycle. Not memoized.
31
+ ErrorVal.from_runtime(kind: "reference_error", **site, operation: operation, input: input, message: "non-productive data cycle")
32
+ when :unforced
33
+ @state = :forcing
34
+ begin
35
+ @value = @compute.call
36
+ rescue ReadFailure => failure
37
+ # Memoize the Ruby error itself. Turn it into a Fusion runtime error below.
38
+ @value = failure
39
+ end
40
+ @state = :done
41
+ @value
42
+ end
43
+
44
+ case result
45
+ when ReadFailure
46
+ ErrorVal.from_runtime(kind: "reference_error", **site, operation: operation, input: input, message: result.message)
47
+ else
48
+ result
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end