diff --git a/deno.json b/deno.json index a9509f4..9657e22 100644 --- a/deno.json +++ b/deno.json @@ -24,7 +24,7 @@ ], "imports": { "$fresh/": "https://deno.land/x/fresh@1.7.3/", - "@swanfactory/hclang": "jsr:@swanfactory/hclang@^0.6.9", + "@swanfactory/hclang": "jsr:@swanfactory/hclang@^0.7.1", "preact": "https://esm.sh/preact@10.22.0", "preact/": "https://esm.sh/preact@10.22.0/", "@preact/signals": "https://esm.sh/*@preact/signals@1.2.2", diff --git a/islands/Interpreter.tsx b/islands/Interpreter.tsx index 8393ae2..f95397b 100644 --- a/islands/Interpreter.tsx +++ b/islands/Interpreter.tsx @@ -1,11 +1,17 @@ -import { useState } from "preact/hooks"; -// import { execute } from "@swanfactory/hclang"; +import { useState, useEffect } from "preact/hooks"; +import { execute } from "@swanfactory/hclang"; + +interface HistoryItem { + code: string; + result: string; + timestamp: number; +} function evaluateCode(code: string): string { console.log(`Evaluating code: ${code}`); - // console.log(execute); + console.log(execute); try { - const result = code.toUpperCase(); + const result = execute(code); console.log(`Result: ${result}`); return result.toString(); } catch (error: unknown) { @@ -19,13 +25,40 @@ export default function Interpreter() { const [error, setError] = useState(""); const [isLoading, setIsLoading] = useState(false); const [result, setResult] = useState(""); + const [history, setHistory] = useState([]); + + useEffect(() => { + const savedHistory = localStorage.getItem('hc-history'); + if (savedHistory) { + setHistory(JSON.parse(savedHistory)); + } + }, []); const handleEvaluation = (code: string): void => { setError(""); setIsLoading(true); try { - const evalResult = evaluateCode(code); + // Build context from history + const context = history + .map(item => item.code) + .reverse() + .join("\n"); + + // Combine history with new code + const fullCode = context ? `${context}\n${code}` : code; + + const evalResult = evaluateCode(fullCode); setResult(evalResult); + + const newHistoryItem = { + code, + result: evalResult, + timestamp: Date.now() + }; + + const updatedHistory = [newHistoryItem, ...history].slice(0, 50); // Keep last 50 items + setHistory(updatedHistory); + localStorage.setItem('hc-history', JSON.stringify(updatedHistory)); } catch (e: unknown) { setError(e instanceof Error ? e.message : String(e)); } finally { @@ -33,6 +66,18 @@ export default function Interpreter() { } }; + const handleHistoryClick = (historyItem: HistoryItem) => { + setText((prev) => prev + "\n" + historyItem.code); + }; + + const clearHistory = () => { + setHistory([]); + setText(""); + setResult(""); + setError(""); + localStorage.removeItem('hc-history'); + }; + return (