Skip to content

Commit 6285611

Browse files
made it easier to create functions
1 parent b2fb628 commit 6285611

File tree

1 file changed

+103
-43
lines changed

1 file changed

+103
-43
lines changed

PlasticEngine.cs

Lines changed: 103 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Text;
5+
using static Plastic.TypeChecker;
56

67
namespace Plastic
78
{
@@ -24,10 +25,34 @@ public class ImportStmt : Stmt
2425

2526
public enum ImportType
2627
{
27-
Import,
28-
Reference,
29-
Using
30-
}
28+
Import,
29+
Reference,
30+
Using
31+
}
32+
33+
// Add the missing _builtinFunctions dictionary to resolve the error
34+
public static Dictionary<string, TypeChecker.TypeInfo> _builtinFunctions = new()
35+
{
36+
{ "print", new TypeChecker.TypeInfo("void", false, null, false, false, 0) },
37+
{ "len", new TypeChecker.TypeInfo("i32", false, null, false, false, 0) },
38+
{ "range", new TypeChecker.TypeInfo("range", false, null, false, false, 0) },
39+
{ "sleep", new TypeChecker.TypeInfo("void", false, null, false, false, 0) },
40+
{ "input", new TypeChecker.TypeInfo("string", false, null, false, false, 0) },
41+
{ "parseInt", new TypeChecker.TypeInfo("f64", false, null, false, false, 0) },
42+
{ "toString", new TypeChecker.TypeInfo("string", false, null, false, false, 0) },
43+
{ "exit", new TypeChecker.TypeInfo("void", false, null, false, false, 0) },
44+
{ "readFile", new TypeChecker.TypeInfo("string", false, null, false, false, 0) },
45+
{ "writeFile", new TypeChecker.TypeInfo("void", false, null, false, false, 0) },
46+
{ "appendFile", new TypeChecker.TypeInfo("void", false, null, false, false, 0) },
47+
{ "deleteFile", new TypeChecker.TypeInfo("void", false, null, false, false, 0) },
48+
{ "exists", new TypeChecker.TypeInfo("bool", false, null, false, false, 0) },
49+
{ "mkdir", new TypeChecker.TypeInfo("void", false, null, false, false, 0) },
50+
{ "rmdir", new TypeChecker.TypeInfo("void", false, null, false, false, 0) },
51+
{ "listDir", new TypeChecker.TypeInfo("List<string>", false, null, false, false, 0) },
52+
{ "copyFile", new TypeChecker.TypeInfo("void", false, null, false, false, 0) },
53+
{ "moveFile", new TypeChecker.TypeInfo("void", false, null, false, false, 0) },
54+
{ "renameFile", new TypeChecker.TypeInfo("void", false, null, false, false, 0) }
55+
};
3156
}
3257

3358
public class Token
@@ -805,7 +830,7 @@ private Token Consume(TokenType type, string message)
805830

806831
public class TypeChecker
807832
{
808-
private record TypeInfo(
833+
public record TypeInfo(
809834
string Type,
810835
bool IsMutable,
811836
string? Owner,
@@ -1572,7 +1597,12 @@ private TypeInfo GetExpressionType(Expr expr, HashSet<string> movedVars)
15721597
return fnInfo.returnType;
15731598
}
15741599

1575-
if (callee.Name == "print") return _voidType;
1600+
// Replace the long if-else chain in GetExpressionType (CallExpr callee) with:
1601+
var builtinType = GetBuiltinFunctionType(callee.Name);
1602+
if (builtinType != null)
1603+
return builtinType;
1604+
1605+
/* if (callee.Name == "print") return _voidType;
15761606
if (callee.Name == "len") return _i32Type;
15771607
if (callee.Name == "range") return new TypeInfo("range", false, null, false, false, 0);
15781608
if (callee.Name == "toString") return _stringType;
@@ -1590,7 +1620,7 @@ private TypeInfo GetExpressionType(Expr expr, HashSet<string> movedVars)
15901620
if (callee.Name == "copyFile") return _voidType;
15911621
if (callee.Name == "moveFile") return _voidType;
15921622
if (callee.Name == "renameFile") return _voidType;
1593-
if(callee.Name == "exit") return _voidType;
1623+
if (callee.Name == "exit") return _voidType;*/
15941624

15951625

15961626
throw new Exception($"Undefined function: {callee.Name}");
@@ -1679,7 +1709,13 @@ private TypeInfo GetExpressionType(Expr expr, HashSet<string> movedVars)
16791709
throw new Exception($"Unsupported expression type: {expr.GetType().Name}");
16801710
}
16811711
}
1682-
1712+
private TypeInfo? GetBuiltinFunctionType(string name)
1713+
{
1714+
// Use the static dictionary from ImportStmt to look up the type
1715+
if (ImportStmt._builtinFunctions.TryGetValue(name, out var type))
1716+
return type;
1717+
return null;
1718+
}
16831719
private bool IsValidType(string type)
16841720
{
16851721
return type == "i32" || type == "f64" || type == "bool" || type == "string" ||
@@ -1739,6 +1775,11 @@ private bool AreTypesCompatible(string sourceType, string targetType)
17391775
_typeCompatibilityCache[key] = false;
17401776
return false;
17411777
}
1778+
1779+
public int getLine()
1780+
{
1781+
return _currentLifetime;
1782+
}
17421783
}
17431784

17441785

@@ -2100,7 +2141,8 @@ public class PlasticEngine
21002141
private readonly Dictionary<string, ProgramNode> _packages = new();
21012142
private readonly Dictionary<string, string> _packagePaths = new();
21022143
public bool shouldLeave = false;
2103-
2144+
public Interpreter terpreter;
2145+
public TypeChecker checker;
21042146
public PlasticEngine()
21052147
{
21062148
RegisterBuiltins();
@@ -2183,38 +2225,39 @@ private string ResolvePackagePath(string packageName)
21832225

21842226
private void RegisterBuiltins()
21852227
{
2186-
_globals["print"] = new Action<object>(o => Console.WriteLine(o));
2187-
_globals["len"] = new Func<string, int>(s => s.Length);
2188-
_globals["range"] = new Func<double, double, object>((start, end) => new { Start = start, End = end });
2189-
_globals["sleep"] = new Action<double>(ms => System.Threading.Thread.Sleep((int)ms));
2190-
_globals["input"] = new Func<string?>(() => Console.ReadLine());
2191-
_globals["parseInt"] = new Func<string, double>(s => {
2228+
WorkingCreateGlobal("print", new Action<object>(o => Console.WriteLine(o)), "void");
2229+
WorkingCreateGlobal("len", new Func<string, int>(s => s.Length), "i32");
2230+
WorkingCreateGlobal("range", new Func<double, double, object>((start, end) => new { Start = start, End = end }), "range");
2231+
WorkingCreateGlobal("sleep", new Action<double>(ms => System.Threading.Thread.Sleep((int)ms)), "void");
2232+
WorkingCreateGlobal("input", new Func<string?>(() => Console.ReadLine()), "string");
2233+
WorkingCreateGlobal("parseInt", new Func<string, double>(s =>
2234+
{
21922235
if (double.TryParse(s, out var result)) return result;
21932236
throw new Exception($"Cannot parse '{s}' as number.");
2194-
});
2195-
_globals["toString"] = new Func<object, string>(o => o.ToString());
2196-
_globals["exit"] = new Action(() =>
2237+
}), "f64");
2238+
WorkingCreateGlobal("toString", new Func<object, string>(o => o.ToString()), "string");
2239+
WorkingCreateGlobal("exit", new Action(() =>
21972240
{
21982241
shouldLeave = true;
2199-
});
2242+
}), "void");
22002243

2201-
_globals["readFile"] = new Func<string, string>(path =>
2244+
WorkingCreateGlobal("readFile", new Func<string, string>(path =>
22022245
{
22032246
if (File.Exists(path))
22042247
{
22052248
return File.ReadAllText(path);
22062249
}
22072250
throw new Exception($"File '{path}' not found.");
2208-
});
2209-
_globals["writeFile"] = new Action<string, string>((path, content) =>
2251+
}), "string");
2252+
WorkingCreateGlobal("writeFile", new Action<string, string>((path, content) =>
22102253
{
22112254
File.WriteAllText(path, content);
2212-
});
2213-
_globals["appendFile"] = new Action<string, string>((path, content) =>
2255+
}), "void");
2256+
WorkingCreateGlobal("appendFile", new Action<string, string>((path, content) =>
22142257
{
22152258
File.AppendAllText(path, content);
2216-
});
2217-
_globals["deleteFile"] = new Action<string>(path =>
2259+
}), "void");
2260+
WorkingCreateGlobal("deleteFile", new Action<string>(path =>
22182261
{
22192262
if (File.Exists(path))
22202263
{
@@ -2224,16 +2267,16 @@ private void RegisterBuiltins()
22242267
{
22252268
throw new Exception($"File '{path}' not found.");
22262269
}
2227-
});
2228-
_globals["exists"] = new Func<string, bool>(path => File.Exists(path));
2229-
_globals["mkdir"] = new Action<string>(path =>
2270+
}), "void");
2271+
WorkingCreateGlobal("exists", new Func<string, bool>(path => File.Exists(path)), "bool");
2272+
WorkingCreateGlobal("mkdir", new Action<string>(path =>
22302273
{
22312274
if (!Directory.Exists(path))
22322275
{
22332276
Directory.CreateDirectory(path);
22342277
}
2235-
});
2236-
_globals["rmdir"] = new Action<string>(path =>
2278+
}), "void");
2279+
WorkingCreateGlobal("rmdir", new Action<string>(path =>
22372280
{
22382281
if (Directory.Exists(path))
22392282
{
@@ -2243,16 +2286,16 @@ private void RegisterBuiltins()
22432286
{
22442287
throw new Exception($"Directory '{path}' not found.");
22452288
}
2246-
});
2247-
_globals["listDir"] = new Func<string, List<string>>(path =>
2289+
}), "void");
2290+
WorkingCreateGlobal("listDir", new Func<string, List<string>>(path =>
22482291
{
22492292
if (Directory.Exists(path))
22502293
{
22512294
return Directory.GetFiles(path).ToList();
22522295
}
22532296
throw new Exception($"Directory '{path}' not found.");
2254-
});
2255-
_globals["copyFile"] = new Action<string, string>((source, destination) =>
2297+
}), "List<string>");
2298+
WorkingCreateGlobal("copyFile", new Action<string, string>((source, destination) =>
22562299
{
22572300
if (File.Exists(source))
22582301
{
@@ -2262,8 +2305,8 @@ private void RegisterBuiltins()
22622305
{
22632306
throw new Exception($"File '{source}' not found.");
22642307
}
2265-
});
2266-
_globals["moveFile"] = new Action<string, string>((source, destination) =>
2308+
}), "void");
2309+
WorkingCreateGlobal("moveFile", new Action<string, string>((source, destination) =>
22672310
{
22682311
if (File.Exists(source))
22692312
{
@@ -2273,8 +2316,8 @@ private void RegisterBuiltins()
22732316
{
22742317
throw new Exception($"File '{source}' not found.");
22752318
}
2276-
});
2277-
_globals["renameFile"] = new Action<string, string>((oldName, newName) =>
2319+
}), "void");
2320+
WorkingCreateGlobal("renameFile", new Action<string, string>((oldName, newName) =>
22782321
{
22792322
if (File.Exists(oldName))
22802323
{
@@ -2284,7 +2327,24 @@ private void RegisterBuiltins()
22842327
{
22852328
throw new Exception($"File '{oldName}' not found.");
22862329
}
2287-
});
2330+
}), "void");
2331+
2332+
WorkingCreateGlobal("breakPoint", new Action(() => Console.WriteLine($"breakpoint hit! Line: {checker.getLine()}")), "void");
2333+
}
2334+
2335+
public void WorkingCreateGlobal(string name, object value, object typeReturn)
2336+
{
2337+
ArgumentNullException.ThrowIfNull(name);
2338+
ArgumentNullException.ThrowIfNull(value);
2339+
if (!ImportStmt._builtinFunctions.ContainsKey(name))
2340+
{
2341+
ImportStmt._builtinFunctions.Add(name, new TypeInfo(typeReturn.ToString(), false, null, false, false, 0));
2342+
}
2343+
else
2344+
{
2345+
ImportStmt._builtinFunctions[name] = new TypeInfo(typeReturn.ToString(), false, null, false, false, 0);
2346+
}
2347+
_globals[name] = value;
22882348
}
22892349

22902350
public object? Evaluate(string source, bool readPlugins = true)
@@ -2302,11 +2362,11 @@ private void RegisterBuiltins()
23022362
ProcessImports(program);
23032363
}
23042364

2305-
var checker = new TypeChecker();
2365+
checker = new TypeChecker();
23062366
checker.Check(program);
23072367

2308-
var interpreter = new Interpreter(_globals);
2309-
return interpreter.Execute(program);
2368+
terpreter = new Interpreter(_globals);
2369+
return terpreter.Execute(program);
23102370
}
23112371
catch (Exception ex)
23122372
{

0 commit comments

Comments
 (0)