types.ts 961 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { Type } from "./type";
  2. import { MultiType } from "./multiType";
  3. import { IType } from "./itype";
  4. import { Maybe } from "../util/maybe";
  5. const INTEGER = new Type("integer", 0);
  6. const REAL = new Type("real", 1);
  7. const STRING = new Type("text", 2);
  8. const BOOLEAN = new Type("boolean", 3);
  9. const CHAR = new Type("char", 4);
  10. const VOID = new Type("void", 5);
  11. const UNDEFINED = new Type("undefined", 6);
  12. const ALL = new MultiType([INTEGER, REAL, STRING, CHAR, BOOLEAN]);
  13. interface TypeMap {
  14. [key: string]: IType;
  15. }
  16. export const Types = Object.freeze({
  17. INTEGER: INTEGER,
  18. REAL: REAL,
  19. STRING: STRING,
  20. CHAR: CHAR,
  21. BOOLEAN: BOOLEAN,
  22. VOID: VOID,
  23. UNDEFINED: UNDEFINED,
  24. ALL: ALL
  25. });
  26. export function fromOrdToType (ord: number): Maybe<IType> {
  27. const typeMap = Types as TypeMap;
  28. for (const t in typeMap) {
  29. if (typeMap[t].ord && typeMap[t].ord === ord){
  30. return Maybe.some<IType>(typeMap[t]);
  31. }
  32. }
  33. return Maybe.none<IType>();
  34. }