Skip to content

Commit

Permalink
adding type reflection operator
Browse files Browse the repository at this point in the history
  • Loading branch information
aslanix committed Mar 18, 2024
1 parent 0c0e8d1 commit e60c6a8
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 2 deletions.
1 change: 1 addition & 0 deletions compiler/src/IR.hs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ instance WellFormedIRCheck IRExpr where
, "fprintlnWithLabels"
, "fwrite"
, "getTime"
, "getType"
, "getNanoTime"
, "getStdout"
, "guard"
Expand Down
6 changes: 4 additions & 2 deletions rt/src/UserRuntime.mts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { RuntimeAssert } from './builtins/runtimeassert.mjs'
import { BuiltinService } from './builtins/service.mjs'
import { BuiltinString } from './builtins/string.mjs'
import { BuiltinRecordReflection } from './builtins/recordReflection.mjs'
import { BuiltinTypeInformation } from './builtins/types.mjs'

let BuiltSpawnSendReceive = x => BuiltinSpawn(BuiltinSend(BuiltinReceive(x)))

Expand Down Expand Up @@ -60,5 +61,6 @@ export const UserRuntime =
BuiltinDebugUtils(
BuiltinMath(
BuiltinRecordReflection(
BuiltinStdIo(UserRuntimeZero)
)))))))))))))))))))))))))))
BuiltinTypeInformation(
BuiltinStdIo(UserRuntimeZero)
))))))))))))))))))))))))))))
76 changes: 76 additions & 0 deletions rt/src/builtins/types.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
'use strict'
import { UserRuntimeZero, Constructor, mkBase } from './UserRuntimeZero.mjs'
import { LVal } from '../Lval.mjs';
import { assertIsNTuple, assertIsRecord, assertIsString, assertIsUnit, assertNormalState } from '../Asserts.mjs'
import { Record } from "../Record.mjs";
import { lub } from '../options.mjs';
import { __unit } from '../UnitVal.mjs';
import { TroupeType } from '../TroupeTypes.mjs';

export function BuiltinTypeInformation<TBase extends Constructor<UserRuntimeZero>>(Base: TBase) {
return class extends Base {
// returns a string containing the type information
getType = mkBase((larg) => {
let _t = "unknown" // 2024-03-18; todo: add proper type
switch (larg.val._troupeType) {
case TroupeType.UNIT:
_t = "unit";
break;
case TroupeType.BOOLEAN:
_t = "boolean";
break;
case TroupeType.NUMBER:
_t = "number";
break;

case TroupeType.STRING:
_t = "string";
break;
case TroupeType.PROCESS_ID:
_t = "process_id";
break;
case TroupeType.LEVEL:
_t = "level";
break;
case TroupeType.AUTHORITY:
_t = "authority";
break;
case TroupeType.CLOSURE:
_t = "function";
break;
case TroupeType.TUPLE:
_t = "tuple";
break;
case TroupeType.LIST:
_t = "list";
break;
case TroupeType.RECORD:
_t = "record";
break;
case TroupeType.LOCALOBJECT:
_t = "localobject";
break;
default:
switch (typeof larg.val) {
case 'string':
_t = "string";
break;
case 'number':
_t = "number";
break;
case 'boolean':
_t = "boolean"
break;
}
}
return this.runtime.ret (
new LVal ( _t
, lub (larg.tlev, this.runtime.$t.pc)
, this.runtime.$t.pc)
)



})
}
}
2 changes: 2 additions & 0 deletions tests/rt/pos/core/gettype.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
2024-03-18T10:10:03.645Z [RTM] info: Skipping network creation. Observe that all external IO operations will yield a runtime error.
>>> Main thread finished with value: ["string"@{}%{}, "function"@{}%{}, "number"@{}%{}, "unit"@{}%{}, "record"@{}%{}]@{}%{}
9 changes: 9 additions & 0 deletions tests/rt/pos/core/gettype.trp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(* we want a simple function
getType x that returns
the information about the type
*)
import lists
let val ls = [ "hello", fn x => x + 1, 2, () , {x = 2}]
in map getType ls
end

0 comments on commit e60c6a8

Please sign in to comment.