From a107048f87828b87b3bd55db1b6217710c40ee84 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Thu, 12 Jan 2023 18:41:49 +0530 Subject: feat: adds mutable reference effects --- src/context.ts | 12 ++++++++++++ src/eval.ts | 27 ++++++++++++++++++++++++++- src/types.ts | 5 +++++ 3 files changed, 43 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/context.ts b/src/context.ts index bdae0d3..6b7875c 100644 --- a/src/context.ts +++ b/src/context.ts @@ -65,6 +65,13 @@ export const createContext = (options: CtxOptions): Ctx => { } } + const refMap: Map = new Map + const createRef = (ty: string): string => { + const key = uuid() + refMap.set(key, ty) + return key + } + const ctx: Ctx = { sourceFile, typeChecker, @@ -72,6 +79,11 @@ export const createContext = (options: CtxOptions): Ctx => { typeToString, getTypeValue, + createRef, + getRef: (k: string) => refMap.get(k), + setRef: (k: string, ty: string) => refMap.set(k, ty), + deleteRef: (k: string) => refMap.delete(k), + createResult, getResultExpr, printResultNode: () => console.log(resultTypeNode?.print()), diff --git a/src/eval.ts b/src/eval.ts index 12997b1..8b53982 100644 --- a/src/eval.ts +++ b/src/eval.ts @@ -4,7 +4,6 @@ import readline from 'readline' import { match } from './util' import { Ctx } from './types' -import { assert } from 'console' const rl = readline.createInterface({ input: process.stdin, @@ -39,6 +38,32 @@ export const evaluateType = async ( return [] }, + CreateRef: async () => { + const val = ctx.typeToString(args[0]) + const refKey = ctx.createRef(val) + const [resultKey, _] = ctx.createResult(JSON.stringify(refKey)) + return [resultKey] + }, + + GetRef: async () => { + const refKey = ctx.getTypeValue(args[0]) + const val = ctx.getRef(refKey) + if (!val) throw new Error('Ref has been deleted') + const [resultKey, _] = ctx.createResult(val) + return [resultKey] + }, + + SetRef: async () => { + const [ keyTy, valTyp ] = args + ctx.setRef(ctx.getTypeValue(keyTy), ctx.typeToString(valTyp)) + return [] + }, + + DeleteRef: async () => { + ctx.deleteRef(ctx.getTypeValue(args[0])) + return [] + }, + Pure: async () => { const [valTyp] = args const [resultKey, _] = ctx.createResult(ctx.typeToString(valTyp)) diff --git a/src/types.ts b/src/types.ts index e3ea7e9..059c0c2 100644 --- a/src/types.ts +++ b/src/types.ts @@ -17,6 +17,11 @@ export interface Ctx { getResultExpr: (key?: string) => string printResultNode: () => void + createRef: (ty: string) => string, + getRef: (key: string) => any, + setRef: (key: string, ty: string) => void, + deleteRef: (key: string) => void, + addCustomEffect: (name: string, expr: string) => void runCustomEffect: (name: string, args: Type[]) => Promise hasCustomEffect: (name: string) => boolean -- cgit v1.3.1