Skip to content

Commit 67a92b4

Browse files
committed
fixing bugs and testing
1 parent 94d0afc commit 67a92b4

6 files changed

Lines changed: 28 additions & 7 deletions

File tree

7Sharp/7sLib/7sLibManager.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ internal static void Save(string path)
1515
{
1616
try
1717
{
18+
string[] splitPath = path.Split('.');
19+
path = string.Join(".", splitPath.Take(splitPath.Length - 1).ToArray()) + ".7slib";
1820
using (ZipArchive zip = ZipFile.Open(path, ZipArchiveMode.Update))
1921
{
2022
ZipArchiveEntry dotText = zip.CreateEntry(".text");
@@ -128,14 +130,12 @@ internal static _7sLibrary Load(string path)
128130

129131
private static string GetSHA1(Stream s) => BitConverter.ToString(SHA1.Create().ComputeHash(s));
130132

131-
private static string GetSHA1(string path)
133+
private static string GetSHA1(string contents)
132134
{
133-
string o;
134-
using (Stream fs = new FileStream(path, FileMode.Open))
135+
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(contents)))
135136
{
136-
o = BitConverter.ToString(SHA1.Create().ComputeHash(fs)); //dont return here let using exit to close stream }
137+
return BitConverter.ToString(SHA1.Create().ComputeHash(ms));
137138
}
138-
return o;
139139
}
140140
}
141141
}

7Sharp/Intrerpreter/InterpreterState.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ public static void Init(ref InterpreterState state)
103103
{
104104
{ 1, new Action<object>(SysFunctions.WriteRaw) }
105105
}));
106+
state.Functions.Add(new _7sFunction("read", new Dictionary<int, Delegate>()
107+
{
108+
{ 0, new Func<string>(SysFunctions.Read) }
109+
}));
106110
state.Functions.Add(new _7sFunction("getLoopIndex", new Dictionary<int, Delegate>()
107111
{
108112
{ 0, new Func<int>(state.GetFirstLoopIndex) },
@@ -233,7 +237,9 @@ public void PopScope()
233237

234238
public void Import(string libPath)
235239
{
240+
#if DEBUG
236241
ColorConsoleMethods.WriteLineColor($"Importing: \"{libPath}\"", ConsoleColor.Magenta);
242+
#endif
237243
InterpreterState state = this;
238244
SysLibrary[] libs = SysLibrary.GetAllLibraries();
239245
if (libs.Any(l => l.GetName() == libPath))

7Sharp/Intrerpreter/Nodes/FunctionDefinitionNode.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ public static bool IsFunctionDefinition(List<Token<TokenType>> tokens)
3636
bool result = true;
3737
result &= tokens.Count >= 4;
3838
result &= tokens[0].TokenID == TokenType.FUNCTION;
39+
if (!result)
40+
{
41+
return false;
42+
}
3943
result &= tokens[1].TokenID == TokenType.IDENTIFIER;
4044
result &= tokens[2].TokenID == TokenType.LPAREN;
4145

7Sharp/Intrerpreter/Nodes/LoopNode.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@ public override void Run(ref InterpreterState state)
3636
{
3737
state.Location = child.linePosition;
3838
child.Run(ref state);
39+
//continue;
3940
if (state.ContinueUsed)
4041
{
4142
state.ContinueUsed = false;
4243
break;
4344
}
45+
//break;
4446
if (state.BreakUsed)
4547
{
4648
state.BreakUsed = false;

7Sharp/Intrerpreter/Sysfunctions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ public static double Double(object value)
4444

4545
public static double Tan(double v) => Math.Tan(v);
4646

47-
public static double Asin(double v) => Math.Asin(v);
47+
public static double Asin(double v) => Math.Asin(Utils.CheckRange(v, -1, 1, "asin: Invalid domain!"));
4848

49-
public static double Acos(double v) => Math.Acos(v);
49+
public static double Acos(double v) => Math.Acos(Utils.CheckRange(v, -1, 1, "acos: Invalid domain!"));
5050

5151
public static double Atan(double v) => Math.Atan(v);
5252

7Sharp/Utils.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,5 +193,14 @@ public static string GetSimpleName(this Type t)
193193
}
194194

195195
public static Dictionary<K, V> Clone<K, V>(this Dictionary<K, V> dict) => dict.AsEnumerable().ToDictionary(kv => kv.Key, kv => kv.Value);
196+
197+
public static double CheckRange(double v, double min, double max, string message)
198+
{
199+
if (v < min || v > max)
200+
{
201+
throw new InterpreterException(message);
202+
}
203+
return v;
204+
}
196205
}
197206
}

0 commit comments

Comments
 (0)