Skip to content

Commit

Permalink
adding record reflection - new function recordExtend and a correspond…
Browse files Browse the repository at this point in the history
…ing simple test
  • Loading branch information
aslanix committed Mar 11, 2024
1 parent e0701c4 commit 0c0e8d1
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 1 deletion.
1 change: 1 addition & 0 deletions compiler/src/IR.hs
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ instance WellFormedIRCheck IRExpr where
, "raiseTrust"
, "random"
, "receive"
, "recordExtend"
, "register"
, "_resetScheduler"
, "rcv"
Expand Down
5 changes: 4 additions & 1 deletion rt/src/UserRuntime.mts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { BuiltinLocalArrays } from './builtins/localarrays.mjs'
import { RuntimeAssert } from './builtins/runtimeassert.mjs'
import { BuiltinService } from './builtins/service.mjs'
import { BuiltinString } from './builtins/string.mjs'
import { BuiltinRecordReflection } from './builtins/recordReflection.mjs'

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

Expand Down Expand Up @@ -58,4 +59,6 @@ export const UserRuntime =
BuiltinPini(
BuiltinDebugUtils(
BuiltinMath(
BuiltinStdIo(UserRuntimeZero)))))))))))))))))))))))))))
BuiltinRecordReflection(
BuiltinStdIo(UserRuntimeZero)
)))))))))))))))))))))))))))
25 changes: 25 additions & 0 deletions rt/src/builtins/recordReflection.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'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';

export function BuiltinRecordReflection<TBase extends Constructor<UserRuntimeZero>>(Base: TBase) {
return class extends Base {
recordExtend = mkBase((larg) => {
assertIsNTuple(larg, 3);
let arg = larg.val;
assertIsRecord(arg[0]);
let raw_rec = arg[0].val;
assertIsString(arg[1]);
let new_field_str = arg[1];
let raw_newfield = new_field_str.val;
let v = new LVal (arg[2].val, lub (arg[2].lev , new_field_str.lev));
let new_raw_rec = Record.mkWithRecord (raw_rec, [[raw_newfield, v ]]);
let ret_val = new LVal (new_raw_rec, lub (this.runtime.$t.pc, larg.lev, new_field_str.lev));
return this.runtime.ret(ret_val);
})
}
}
2 changes: 2 additions & 0 deletions tests/rt/pos/core/record-extend.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
2024-03-11T10:25:40.461Z [RTM] info: Skipping network creation. Observe that all external IO operations will yield a runtime error.
>>> Main thread finished with value: {b=2@{}%{}, a=10@{}%{}, c=55@{alice}%{alice}}@{alice}%{alice}
8 changes: 8 additions & 0 deletions tests/rt/pos/core/record-extend.trp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
let val r = {}
val r2 = {r with b = 2}
val field = "a"
val value = 10
val r3 = recordExtend (r2, field, value)
val field2 = "c" raisedTo `{alice}`
in recordExtend (r3, field2, 55)
end

0 comments on commit 0c0e8d1

Please sign in to comment.