@forthic/interp 0.2.0 → 0.4.0

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 (35) hide show
  1. package/dist/cjs/forthic/errors.d.ts +70 -0
  2. package/dist/cjs/forthic/errors.js +174 -0
  3. package/dist/cjs/forthic/errors.js.map +1 -0
  4. package/dist/cjs/forthic/global_module.d.ts +2 -1
  5. package/dist/cjs/forthic/global_module.js +11 -4
  6. package/dist/cjs/forthic/global_module.js.map +1 -1
  7. package/dist/cjs/forthic/interpreter.d.ts +12 -1
  8. package/dist/cjs/forthic/interpreter.js +75 -44
  9. package/dist/cjs/forthic/interpreter.js.map +1 -1
  10. package/dist/cjs/forthic/module.js +2 -4
  11. package/dist/cjs/forthic/module.js.map +1 -1
  12. package/dist/cjs/forthic/tokenizer.d.ts +21 -2
  13. package/dist/cjs/forthic/tokenizer.js +57 -21
  14. package/dist/cjs/forthic/tokenizer.js.map +1 -1
  15. package/dist/cjs/index.d.ts +1 -0
  16. package/dist/cjs/index.js +1 -0
  17. package/dist/cjs/index.js.map +1 -1
  18. package/dist/esm/forthic/errors.d.ts +70 -0
  19. package/dist/esm/forthic/errors.js +174 -0
  20. package/dist/esm/forthic/errors.js.map +1 -0
  21. package/dist/esm/forthic/global_module.d.ts +2 -1
  22. package/dist/esm/forthic/global_module.js +11 -4
  23. package/dist/esm/forthic/global_module.js.map +1 -1
  24. package/dist/esm/forthic/interpreter.d.ts +12 -1
  25. package/dist/esm/forthic/interpreter.js +75 -44
  26. package/dist/esm/forthic/interpreter.js.map +1 -1
  27. package/dist/esm/forthic/module.js +2 -4
  28. package/dist/esm/forthic/module.js.map +1 -1
  29. package/dist/esm/forthic/tokenizer.d.ts +21 -2
  30. package/dist/esm/forthic/tokenizer.js +57 -21
  31. package/dist/esm/forthic/tokenizer.js.map +1 -1
  32. package/dist/esm/index.d.ts +1 -0
  33. package/dist/esm/index.js +1 -0
  34. package/dist/esm/index.js.map +1 -1
  35. package/package.json +1 -4
@@ -0,0 +1,70 @@
1
+ import { CodeLocation } from "./tokenizer";
2
+ declare class ForthicError extends Error {
3
+ private note;
4
+ private location?;
5
+ constructor(note: string, location?: CodeLocation);
6
+ getLocation(): CodeLocation;
7
+ getNote(): string;
8
+ getMessage(): string;
9
+ }
10
+ export declare class UnknownWordError extends ForthicError {
11
+ private word;
12
+ constructor(word: string, location?: CodeLocation);
13
+ getWord(): string;
14
+ }
15
+ export declare class InvalidVariableNameError extends ForthicError {
16
+ private var_name;
17
+ private addl_note;
18
+ constructor(var_name: string, addl_note: string, location?: CodeLocation);
19
+ getVarName(): string;
20
+ getAddlNote(): string;
21
+ }
22
+ export declare class UnknownScreenError extends ForthicError {
23
+ private screen_name;
24
+ constructor(screen_name: string, location?: CodeLocation);
25
+ getScreenName(): string;
26
+ }
27
+ export declare class UnknownModuleError extends ForthicError {
28
+ private module_name;
29
+ constructor(module_name: string, location?: CodeLocation);
30
+ getModuleName(): string;
31
+ }
32
+ export declare class TooManyAttemptsError extends ForthicError {
33
+ private num_attempts;
34
+ private max_attempts;
35
+ constructor(num_attempts: number, max_attempts: number, location?: CodeLocation);
36
+ getNumAttempts(): number;
37
+ getMaxAttempts(): number;
38
+ }
39
+ export declare class WordExecutionError extends ForthicError {
40
+ private word_name;
41
+ private error;
42
+ constructor(word_name: string, error: Error, location?: CodeLocation);
43
+ getWordName(): string;
44
+ getError(): Error;
45
+ }
46
+ export declare class ModuleError extends ForthicError {
47
+ private module_name;
48
+ private error;
49
+ constructor(module_name: string, error: Error, location?: CodeLocation);
50
+ getModuleName(): string;
51
+ getError(): Error;
52
+ }
53
+ export declare class StackUnderflowError extends ForthicError {
54
+ constructor(location?: CodeLocation);
55
+ }
56
+ export declare class UnknownTokenError extends ForthicError {
57
+ private token;
58
+ constructor(token: string, location?: CodeLocation);
59
+ getToken(): string;
60
+ }
61
+ export declare class MissingSemicolonError extends ForthicError {
62
+ constructor(location?: CodeLocation);
63
+ }
64
+ export declare class ExtraSemicolonError extends ForthicError {
65
+ constructor(location?: CodeLocation);
66
+ }
67
+ export declare class IntentionalStopError extends Error {
68
+ constructor(message: string);
69
+ }
70
+ export {};
@@ -0,0 +1,174 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.IntentionalStopError = exports.ExtraSemicolonError = exports.MissingSemicolonError = exports.UnknownTokenError = exports.StackUnderflowError = exports.ModuleError = exports.WordExecutionError = exports.TooManyAttemptsError = exports.UnknownModuleError = exports.UnknownScreenError = exports.InvalidVariableNameError = exports.UnknownWordError = void 0;
4
+ class ForthicError extends Error {
5
+ constructor(note, location) {
6
+ const location_note = location ? ` at ${location.screen_name}:${location.line}:${location.column}` : "";
7
+ const message = `${note}${location_note}`;
8
+ super(message);
9
+ this.note = note;
10
+ this.location = location;
11
+ }
12
+ getLocation() {
13
+ return this.location;
14
+ }
15
+ getNote() {
16
+ return this.note;
17
+ }
18
+ getMessage() {
19
+ return this.message;
20
+ }
21
+ }
22
+ // ============================================================
23
+ // Interpreter errors
24
+ // Interpreter couldn't find word in dictionaries
25
+ class UnknownWordError extends ForthicError {
26
+ constructor(word, location) {
27
+ const note = `Unknown word: ${word}`;
28
+ super(note, location);
29
+ this.word = word;
30
+ this.name = "UnknownWordError";
31
+ }
32
+ getWord() {
33
+ return this.word;
34
+ }
35
+ }
36
+ exports.UnknownWordError = UnknownWordError;
37
+ // Variable name is not allowed
38
+ class InvalidVariableNameError extends ForthicError {
39
+ constructor(var_name, addl_note, location) {
40
+ const note = `Invalid variable name: ${var_name}(${addl_note})`;
41
+ super(note, location);
42
+ this.var_name = var_name;
43
+ this.addl_note = addl_note;
44
+ this.name = "InvalidVariableNameError";
45
+ }
46
+ getVarName() {
47
+ return this.var_name;
48
+ }
49
+ getAddlNote() {
50
+ return this.addl_note;
51
+ }
52
+ }
53
+ exports.InvalidVariableNameError = InvalidVariableNameError;
54
+ // Interpreter couldn't find screen
55
+ class UnknownScreenError extends ForthicError {
56
+ constructor(screen_name, location) {
57
+ const note = `Unknown screen: ${screen_name}`;
58
+ super(note, location);
59
+ this.screen_name = screen_name;
60
+ this.name = "UnknownScreenError";
61
+ }
62
+ getScreenName() {
63
+ return this.screen_name;
64
+ }
65
+ }
66
+ exports.UnknownScreenError = UnknownScreenError;
67
+ // Interpreter couldn't find module
68
+ class UnknownModuleError extends ForthicError {
69
+ constructor(module_name, location) {
70
+ const note = `Unknown module: ${module_name}`;
71
+ super(note, location);
72
+ this.module_name = module_name;
73
+ this.name = "UnknownModuleError";
74
+ }
75
+ getModuleName() {
76
+ return this.module_name;
77
+ }
78
+ }
79
+ exports.UnknownModuleError = UnknownModuleError;
80
+ class TooManyAttemptsError extends ForthicError {
81
+ constructor(num_attempts, max_attempts, location) {
82
+ const note = `Too many recovery attempts: ${num_attempts} of ${max_attempts}`;
83
+ super(note, location);
84
+ this.num_attempts = num_attempts;
85
+ this.max_attempts = max_attempts;
86
+ this.name = "TooManyAttemptsError";
87
+ }
88
+ getNumAttempts() {
89
+ return this.num_attempts;
90
+ }
91
+ getMaxAttempts() {
92
+ return this.max_attempts;
93
+ }
94
+ }
95
+ exports.TooManyAttemptsError = TooManyAttemptsError;
96
+ class WordExecutionError extends ForthicError {
97
+ constructor(word_name, error, location) {
98
+ const note = `Error executing word: ${word_name}`;
99
+ super(note, location);
100
+ this.word_name = word_name;
101
+ this.error = error;
102
+ this.name = "WordExecutionError";
103
+ }
104
+ getWordName() {
105
+ return this.word_name;
106
+ }
107
+ getError() {
108
+ return this.error;
109
+ }
110
+ }
111
+ exports.WordExecutionError = WordExecutionError;
112
+ class ModuleError extends ForthicError {
113
+ constructor(module_name, error, location) {
114
+ const note = `Error in module ${module_name}`;
115
+ super(note, location);
116
+ this.module_name = module_name;
117
+ this.error = error;
118
+ this.name = "ModuleError";
119
+ }
120
+ getModuleName() {
121
+ return this.module_name;
122
+ }
123
+ getError() {
124
+ return this.error;
125
+ }
126
+ }
127
+ exports.ModuleError = ModuleError;
128
+ class StackUnderflowError extends ForthicError {
129
+ constructor(location) {
130
+ const note = `Stack underflow`;
131
+ super(note, location);
132
+ this.name = "StackUnderflowError";
133
+ }
134
+ }
135
+ exports.StackUnderflowError = StackUnderflowError;
136
+ class UnknownTokenError extends ForthicError {
137
+ constructor(token, location) {
138
+ const note = `(Should never happen) Unknown type of token: ${token}`;
139
+ super(note, location);
140
+ this.token = token;
141
+ this.name = "UnknownTokenError";
142
+ }
143
+ getToken() {
144
+ return this.token;
145
+ }
146
+ }
147
+ exports.UnknownTokenError = UnknownTokenError;
148
+ class MissingSemicolonError extends ForthicError {
149
+ constructor(location) {
150
+ const note = `Missing semicolon -- new definition started before previous one was complete`;
151
+ super(note, location);
152
+ this.name = "MissingSemicolonError";
153
+ }
154
+ }
155
+ exports.MissingSemicolonError = MissingSemicolonError;
156
+ class ExtraSemicolonError extends ForthicError {
157
+ constructor(location) {
158
+ const note = `Extra semicolon -- no definition in progress`;
159
+ super(note, location);
160
+ this.name = "ExtraSemicolonError";
161
+ }
162
+ }
163
+ exports.ExtraSemicolonError = ExtraSemicolonError;
164
+ // ============================================================
165
+ // Misc errors
166
+ // Error indicating intentional stop
167
+ class IntentionalStopError extends Error {
168
+ constructor(message) {
169
+ super(message);
170
+ this.name = "IntentionalStopError";
171
+ }
172
+ }
173
+ exports.IntentionalStopError = IntentionalStopError;
174
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../../../src/forthic/errors.ts"],"names":[],"mappings":";;;AAEA,MAAM,YAAa,SAAQ,KAAK;IAC5B,YAAoB,IAAY,EAAU,QAAuB;QAC7D,MAAM,aAAa,GAAG,QAAQ,CAAC,CAAC,CAAC,OAAO,QAAQ,CAAC,WAAW,IAAI,QAAQ,CAAC,IAAI,IAAI,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QACxG,MAAM,OAAO,GAAG,GAAG,IAAI,GAAG,aAAa,EAAE,CAAC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C;IACtB,CAAC;CACJ;AAVD,8CAUC;AAED,MAAa,qBAAsB,SAAQ,YAAY;IACnD,YAAY,QAAuB;QAC/B,MAAM,IAAI,GAAG,8EAA8E,CAAC;QAC5F,KAAK,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;QACtB,IAAI,CAAC,IAAI,GAAG,uBAAuB,CAAC;IACxC,CAAC;CACJ;AAND,sDAMC;AAED,MAAa,mBAAoB,SAAQ,YAAY;IACjD,YAAY,QAAuB;QAC/B,MAAM,IAAI,GAAG,8CAA8C,CAAC;QAC5D,KAAK,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;QACtB,IAAI,CAAC,IAAI,GAAG,qBAAqB,CAAC;IACtC,CAAC;CACJ;AAND,kDAMC;AAED,+DAA+D;AAC/D,cAAc;AAEd,oCAAoC;AACpC,MAAa,oBAAqB,SAAQ,KAAK;IAC7C,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,sBAAsB,CAAC;IACrC,CAAC;CACF;AALD,oDAKC"}
@@ -1,5 +1,5 @@
1
1
  import { Module, Word } from "./module";
2
- import type { Interpreter } from "./interpreter";
2
+ import { type Interpreter } from "./interpreter";
3
3
  export declare class GlobalModule extends Module {
4
4
  module_id: string;
5
5
  literal_handlers: Array<(value: any) => any>;
@@ -163,5 +163,6 @@ export declare class GlobalModule extends Module {
163
163
  word_JSON_to(interp: Interpreter): void;
164
164
  word_LOAD_SCREEN(interp: Interpreter): Promise<void>;
165
165
  word_dot_s(interp: Interpreter): void;
166
+ word_dot_S(interp: Interpreter): void;
166
167
  word_minus(interp: Interpreter): void;
167
168
  }
@@ -2,10 +2,10 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.GlobalModule = void 0;
4
4
  const module_1 = require("./module");
5
- const ForthicError_1 = require("./ForthicError");
6
5
  const utils_1 = require("./utils");
7
6
  const map_word_1 = require("./global_module/map_word");
8
7
  const tokenizer_1 = require("./tokenizer");
8
+ const errors_1 = require("./errors");
9
9
  const DLE = String.fromCharCode(16); // ASCII char for "Data Link Escape" used as an untypeable quote
10
10
  class GlobalModule extends module_1.Module {
11
11
  constructor(interp) {
@@ -119,6 +119,7 @@ class GlobalModule extends module_1.Module {
119
119
  this.add_module_word("JSON>", this.word_JSON_to);
120
120
  this.add_module_word("LOAD-SCREEN", this.word_LOAD_SCREEN);
121
121
  this.add_module_word(".s", this.word_dot_s);
122
+ this.add_module_word(".S", this.word_dot_S);
122
123
  // --------------------
123
124
  // Date/time words
124
125
  this.add_module_word("AM", this.word_AM);
@@ -291,7 +292,7 @@ class GlobalModule extends module_1.Module {
291
292
  const module = interp.cur_module();
292
293
  varnames.forEach((v) => {
293
294
  if (v.match(/__.*/)) {
294
- throw new ForthicError_1.ForthicError("global_module-696", `word_VARIABLES: variable names cannot begin with '__': '${JSON.stringify(v)}'`, "This is a reserved variable naming convention");
295
+ throw new errors_1.InvalidVariableNameError(v, "Variable names cannot begin with '__'", interp.get_string_location());
295
296
  }
296
297
  module.add_variable(v);
297
298
  });
@@ -2375,14 +2376,20 @@ class GlobalModule extends module_1.Module {
2375
2376
  }
2376
2377
  // ( -- )
2377
2378
  word_dot_s(interp) {
2378
- const stack = interp.stack;
2379
+ const stack = interp.get_stack();
2379
2380
  if (stack.length > 0) {
2380
2381
  console.log(stack[stack.length - 1]);
2381
2382
  }
2382
2383
  else {
2383
2384
  console.log("<STACK EMPTY>");
2384
2385
  }
2385
- interp.halt();
2386
+ throw new errors_1.IntentionalStopError(".s");
2387
+ }
2388
+ // ( -- )
2389
+ word_dot_S(interp) {
2390
+ const stack = interp.get_stack().reverse();
2391
+ console.log(JSON.stringify(stack, null, 2));
2392
+ throw new errors_1.IntentionalStopError(".S");
2386
2393
  }
2387
2394
  // ( a b -- a - b )
2388
2395
  word_minus(interp) {