Skip to content
This repository was archived by the owner on Feb 21, 2026. It is now read-only.

Commit d79caf9

Browse files
committed
Small optimizations
1 parent 25d9446 commit d79caf9

13 files changed

Lines changed: 130 additions & 109 deletions

File tree

eiger/Execution/BuiltInTypes/Array.cs

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,42 @@
77

88
namespace EigerLang.Execution.BuiltInTypes;
99

10+
using System.Text;
11+
1012
class Array : Value
1113
{
12-
public Value[] array;
14+
public List<Value> array;
1315

14-
public Array(string fn, int ln, int ps, Value[] array) : base(fn, ln, ps)
16+
public Array(string fn, int ln, int ps, List<Value> array) : base(fn, ln, ps)
1517
{
1618
this.array = array;
17-
this.filename = fn;
18-
this.line = ln;
19-
this.pos = ps;
2019
}
2120

2221
public override Value AddedTo(object other)
2322
{
2423
if (other is Array arr)
2524
{
26-
return new Array(filename, line, pos, [.. array, .. arr.array]);
25+
var newArr = new List<Value>(array.Count + arr.array.Count);
26+
newArr.AddRange(array);
27+
newArr.AddRange(arr.array);
28+
return new Array(filename, line, pos, newArr);
2729
}
2830
else
2931
{
30-
return new Array(filename, line, pos, [.. array, (Value)other]);
32+
var newArr = new List<Value>(array.Count + 1);
33+
newArr.AddRange(array);
34+
newArr.Add((Value)other);
35+
return new Array(filename, line, pos, newArr);
3136
}
3237
}
3338

3439
public override Boolean ComparisonEqeq(object other)
3540
{
3641
if (other is Array arr)
3742
{
38-
return new Boolean(filename, line, pos, arr.array.Length == array.Length && arr.array.SequenceEqual(array));
39-
}
40-
else
41-
{
42-
return new Boolean(filename, line, pos, false);
43+
return new Boolean(filename, line, pos, arr.array.Count == array.Count && arr.array.SequenceEqual(array));
4344
}
45+
return new Boolean(filename, line, pos, false);
4446
}
4547

4648
public override Boolean ComparisonNeqeq(object other)
@@ -49,53 +51,53 @@ public override Boolean ComparisonNeqeq(object other)
4951
{
5052
return new Boolean(filename, line, pos, !arr.array.SequenceEqual(array));
5153
}
52-
else
53-
{
54-
return new Boolean(filename, line, pos, true);
55-
}
54+
return new Boolean(filename, line, pos, true);
5655
}
5756

58-
public override Value GetIndex(int idx)
57+
private void ValidateIndex(int idx)
5958
{
60-
if (idx < 0 || idx >= array.Length)
59+
if (idx < 0 || idx >= array.Count)
6160
throw new EigerError(filename, line, pos, "Index outside of bounds", EigerError.ErrorType.IndexError);
62-
return array[idx];
6361
}
6462

65-
public override Value GetAttr(ASTNode attr)
63+
public override Value GetIndex(int idx)
6664
{
67-
if (attr.value == "type")
68-
{
69-
return new String(filename, line, pos, "array");
70-
}
71-
return base.GetAttr(attr);
65+
ValidateIndex(idx);
66+
return array[idx];
7267
}
7368

7469
public override void SetIndex(int idx, Value val)
7570
{
76-
if (idx < 0 || idx >= array.Length)
77-
throw new EigerError(filename, line, pos, "Index outside of bounds", EigerError.ErrorType.IndexError);
71+
ValidateIndex(idx);
7872
val.modifiers = array[idx].modifiers;
7973
array[idx] = val;
8074
}
8175

8276
public override Value GetLength()
8377
{
84-
return new Number(filename, line, pos, array.Length);
78+
return new Number(filename, line, pos, array.Count);
79+
}
80+
81+
public override Value GetAttr(ASTNode attr)
82+
{
83+
if (attr.value == "type")
84+
{
85+
return new String(filename, line, pos, "array");
86+
}
87+
return base.GetAttr(attr);
8588
}
8689

8790
public override string ToString()
8891
{
89-
string strep = "[";
90-
for (int i = 0; i < array.Length; i++)
92+
var sb = new StringBuilder();
93+
sb.Append('[');
94+
for (int i = 0; i < array.Count; i++)
9195
{
92-
strep += array[i].ToString();
93-
if (i != array.Length - 1)
94-
{
95-
strep += ", ";
96-
}
96+
sb.Append(array[i].ToString());
97+
if (i != array.Count - 1)
98+
sb.Append(", ");
9799
}
98-
strep += "]";
99-
return strep;
100+
sb.Append(']');
101+
return sb.ToString();
100102
}
101-
}
103+
}

eiger/Execution/BuiltInTypes/Boolean.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ public class Boolean : Value
1212

1313
public Boolean(string filename, int line, int pos, bool value) : base(filename, line, pos)
1414
{
15-
this.filename = filename;
16-
this.value = value;
17-
this.line = line;
18-
this.pos = pos;
1915
this.value = value;
2016
}
2117

eiger/Execution/BuiltInTypes/Class.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ public Class(string filename, int line, int pos, string name, ASTNode blockNode)
1212
{
1313
if (name == "new")
1414
throw new EigerLang.Errors.EigerError(filename, line, pos, "`new` is an invalid name for a class", Errors.EigerError.ErrorType.RuntimeError);
15-
this.filename = filename;
16-
this.line = line;
17-
this.pos = pos;
1815
this.name = name;
1916
this.blockNode = blockNode;
2017
}

eiger/Execution/BuiltInTypes/Dataclass.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ public class Dataclass : Value
99

1010
public Dataclass(string filename, int line, int pos, string name, SymbolTable symbolTable, ASTNode blockNode) : base(filename, line, pos)
1111
{
12-
this.filename = filename;
13-
this.line = line;
14-
this.pos = pos;
1512
this.name = name;
1613

1714
// create local symbol table

eiger/Execution/BuiltInTypes/Instance.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ public class Instance : Value
1111

1212
public Instance(string filename, int line, int pos, Class createdFrom, SymbolTable symbolTable) : base(filename, line, pos)
1313
{
14-
this.filename = filename;
15-
this.line = line;
16-
this.pos = pos;
1714
this.name = createdFrom.name;
1815
this.createdFrom = createdFrom;
1916
this.symbolTable = symbolTable;

eiger/Execution/BuiltInTypes/Nix.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ class Nix : Value
88

99
public Nix(string filename, int line, int pos) : base(filename, line, pos)
1010
{
11-
this.filename = filename;
12-
this.line = line;
13-
this.pos = pos;
1411
}
1512

1613
public override string ToString()

eiger/Execution/BuiltInTypes/Number.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ class Number : Value
1212

1313
public Number(string filename, int line, int pos, double value) : base(filename, line, pos)
1414
{
15-
this.filename = filename;
16-
this.line = line;
17-
this.pos = pos;
1815
this.value = value;
1916
}
2017

eiger/Execution/BuiltInTypes/String.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ class String : Value
1414
public String(string filename, int line, int pos, string value) : base(filename, line, pos)
1515
{
1616
this.value = value;
17-
this.filename = filename;
18-
this.line = line;
19-
this.pos = pos;
2017
}
2118

2219
public override Value GetIndex(int idx)

eiger/Execution/BuiltInTypes/Value.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ namespace EigerLang.Execution.BuiltInTypes;
55

66
public class Value(string _filename, int _line, int _pos)
77
{
8-
public List<string> modifiers = [];
8+
private List<string>? _modifiers;
9+
public List<string> modifiers
10+
{
11+
get => _modifiers ??= new List<string>();
12+
set => _modifiers = value;
13+
}
14+
915
public string filename = _filename;
1016
public int line = _line, pos = _pos;
1117

@@ -101,7 +107,7 @@ public virtual void SetIndex(int idx, Value val)
101107

102108
public virtual Value GetLength()
103109
{
104-
throw new EigerError(filename, line, pos, "Object has no length", EigerError.ErrorType.RuntimeError);
110+
throw new EigerError(filename, line, pos, "Object of type {this.GetType().Name} has no length", EigerError.ErrorType.RuntimeError);
105111
}
106112

107113
public virtual Value GetAttr(ASTNode attr)
@@ -142,13 +148,10 @@ public static Value ToEigerValue(string filename, int line, int pos, dynamic val
142148

143149
public override bool Equals(object? obj)
144150
{
145-
if (obj is not null)
146-
return ComparisonEqeq(obj).value;
147-
else
148-
return false;
151+
if (ReferenceEquals(this, obj)) return true;
152+
if (obj is null || obj.GetType() != this.GetType()) return false;
153+
return ComparisonEqeq(obj).value;
149154
}
150155

151-
public override int GetHashCode() {
152-
return base.GetHashCode();
153-
}
156+
public override int GetHashCode() => HashCode.Combine(filename, line, pos);
154157
}

eiger/Execution/Interpreter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,7 @@ static ReturnResult VisitBinOpNode(ASTNode node, SymbolTable symbolTable)
524524
"/=" => HandleCompoundAssignment(node, rightSide, symbolTable, (left, right) => left.DivedBy(right)),
525525
_ => throw new EigerError(node.filename, node.line, node.pos, $"{Globals.InvalidOperationStr}: {node.value}", EigerError.ErrorType.InvalidOperationError),
526526
};
527+
527528
return new()
528529
{
529530
result = retVal

0 commit comments

Comments
 (0)