Skip to content

Commit ff2f248

Browse files
zhiyuuuuEdwinaZhuygrx532Ex10si0n
authored
Add Typescript Basic features (#141)
*Issue #, if available:* *Description of changes:* Batch of changes from CMU Practicum students to support TS. By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice. --------- Signed-off-by: YingZhu <yingzhu3@andrew.cmu.edu> Co-authored-by: YingZhu <yingzhu3@andrew.cmu.edu> Co-authored-by: ygrx532 <xiayuyang_1506@outlook.com> Co-authored-by: ex10si0n <me@aspires.cc>
1 parent b0d4488 commit ff2f248

20 files changed

+856
-186
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,10 @@
55
vcs/*.smt2
66

77
Strata.code-workspace
8+
9+
conformance_testing/failures/*
10+
package.json
11+
package-lock.json
12+
13+
conformance_testing/__pycache__
14+
conformance_testing/failures

Strata/DL/CallHeap/CallHeapEvaluator.lean

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,19 @@ partial def evalStatementWithContext (stmt : CallHeapStrataStatement) (ctx : Cal
169169
ctx
170170
| .goto _ _ =>
171171
ctx
172-
| .loop _ _ _ _ _ =>
173-
ctx
172+
| .loop guard _ _ body _ =>
173+
let rec loop (ctx : CallHeapEvalContext) : CallHeapEvalContext :=
174+
let (newState, condVal) := Heap.evalHExpr ctx.hstate guard
175+
let conditionIsTrue := match condVal with
176+
| .lambda (.const "true" _) => true
177+
| _ => false
178+
let newCtx := { ctx with hstate := newState }
179+
if conditionIsTrue then
180+
let afterBody := evalProgramWithContext body.ss newCtx
181+
loop afterBody
182+
else
183+
newCtx
184+
loop ctx
174185
| _ =>
175186
ctx
176187

@@ -194,7 +205,9 @@ def evalCallHeapTranslatedProgram (transCtx : CallHeapTranslationContext) (stmts
194205
evalProgramWithContext stmts evalCtx
195206

196207
-- JSON output function with TranslationContext (matching MIDI pattern)
197-
def runCallHeapAndShowJSONWithTranslation (transCtx : CallHeapTranslationContext) (stmts : List CallHeapStrataStatement) : IO Unit := do
208+
def runCallHeapAndShowJSONWithTranslation (transCtx : CallHeapTranslationContext)
209+
(stmts : List CallHeapStrataStatement)
210+
(visibleVars : Option (Std.HashSet String) := none) : IO Unit := do
198211
let finalContext := evalCallHeapTranslatedProgram transCtx stmts
199212
let finalState := finalContext.hstate
200213

@@ -221,8 +234,12 @@ def runCallHeapAndShowJSONWithTranslation (transCtx : CallHeapTranslationContext
221234
| none => continue
222235
| none => continue
223236

237+
-- Optionally filter the visible variables to match a provided allowlist
224238
-- Create JSON object and output (sorted for consistency)
225-
let sortedFields := jsonFields.toArray.qsort (fun a b => a.1 < b.1) |>.toList
239+
let filteredFields : List (String × Lean.Json) := match visibleVars with
240+
| some allowed => jsonFields.filter (fun (name, _) => allowed.contains name)
241+
| none => jsonFields
242+
let sortedFields := filteredFields.toArray.qsort (fun a b => a.1 < b.1) |>.toList
226243
let jsonObj := Lean.Json.mkObj sortedFields
227244
IO.println jsonObj.compress -- Use compress for compact output like {"x": 5, "y": 15}
228245

Strata/DL/Heap/HEval.lean

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,13 @@ partial def evalApp (state : HState) (originalExpr e1 e2 : HExpr) : HState × HE
149149
-- Third application to DynamicFieldAssign - now we can evaluate
150150
-- This handles obj[field] = value where field is dynamic
151151
evalDynamicFieldAssign state2 objExpr fieldExpr e2'
152+
| .deferredOp "StringFieldAccess" _ =>
153+
-- First application to StringFieldAccess - return partially applied
154+
(state2, .app (.deferredOp "StringFieldAccess" none) e2')
155+
| .app (.deferredOp "StringFieldAccess" _) objExpr =>
156+
-- Second application to StringFieldAccess - return partially applied
157+
-- This handles str.fieldName where fieldName is a string literal
158+
evalStringFieldAccess state2 objExpr e2'
152159
| .deferredOp op _ =>
153160
-- First application to a deferred operation - return partially applied
154161
(state2, .app (.deferredOp op none) e2')
@@ -192,6 +199,26 @@ partial def evalDynamicFieldAssign (state : HState) (objExpr fieldExpr valueExpr
192199
-- Can't extract a numeric field index, return error
193200
(state, .lambda (LExpr.const "error_dynamic_field_assign_failed" none))
194201

202+
-- Handle string field access: str.fieldName where fieldName is a string literal
203+
partial def evalStringFieldAccess (state : HState) (objExpr fieldExpr : HExpr) : HState × HExpr :=
204+
match fieldExpr with
205+
| .lambda (LExpr.const key _) =>
206+
if key == "length" then
207+
-- Handle string length property
208+
match objExpr with
209+
| .lambda (LExpr.const s _) =>
210+
let len := s.length
211+
(state, .lambda (LExpr.const (toString len) (some Lambda.LMonoTy.int)))
212+
| _ =>
213+
-- Not a string value
214+
(state, .lambda (LExpr.const "error_not_a_string" none))
215+
else
216+
-- Unknown string property
217+
(state, .lambda (LExpr.const "error_unknown_string_property" none))
218+
| _ =>
219+
-- Field is not a string literal
220+
(state, .lambda (LExpr.const "error_non_literal_string_field" none))
221+
195222
-- Extract a numeric field index from an expression
196223
partial def extractFieldIndex (expr : HExpr) : Option Nat :=
197224
match expr with

Strata/DL/Lambda/IntBoolFactory.lean

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ def intGeFunc [Coe String Ident] : LFunc Ident :=
139139
binaryPredicate "Int.Ge" .int
140140
(some (binOpCeval Int Bool LExpr.denoteInt (fun x y => x >= y) .bool))
141141

142+
def intEqFunc [Coe String Ident] : LFunc Ident :=
143+
binaryPredicate "Int.Eq" .int
144+
(some (binOpCeval Int Bool LExpr.denoteInt (fun x y => x == y) .bool))
145+
142146
/- Boolean Operations -/
143147
def boolAndFunc [Coe String Ident] : LFunc Ident :=
144148
binaryOp "Bool.And" .bool
@@ -172,6 +176,7 @@ def IntBoolFactory : @Factory String :=
172176
intLeFunc,
173177
intGtFunc,
174178
intGeFunc,
179+
intEqFunc,
175180

176181
boolAndFunc,
177182
boolOrFunc,

0 commit comments

Comments
 (0)