Explorar el Código

Implement Either function type for future error processing

Lucas de Souza hace 5 años
padre
commit
b994591564
Se han modificado 2 ficheros con 21 adiciones y 1 borrados
  1. 1 1
      js/ast/error/syntaxErrorFactory.js
  2. 20 0
      js/util/either.ts

+ 1 - 1
js/ast/error/syntaxErrorFactory.js

@@ -73,7 +73,7 @@ export const SyntaxErrorFactory = Object.freeze({
     const context = [token.text, token.line, token.column];
     return createError("duplicate_variable", context);
   },
-  invalid_character: (text, line, column) => {
+  invalid_character: (text, line, _column) => {
     const context = [text, line];
     return createError("invalid_character", context);
   },

+ 20 - 0
js/util/either.ts

@@ -0,0 +1,20 @@
+type Left<T> = { tag: "left", value: T };
+type Right<T> = { tag: "right", value: T };
+export type Either<L, R> = Left<L> | Right<R>;
+
+export function left<T> (value: T): Left<T> {
+  return {tag: "left", value: value}
+}
+
+export function right<T> (value: T): Right<T> {
+  return {tag: "right", value: value}
+}
+
+export function match<T, L, R> (input: Either<L, R>, left: (l: L) => T, right: (right: R) => T) {
+  switch(input.tag) {
+    case "left":
+      return left(input.value);
+    case "right":
+      return right(input.value);
+  }
+}