Skip to content

Commit

Permalink
Fixed indexing and implemented concatenation
Browse files Browse the repository at this point in the history
  • Loading branch information
TheUnlocked committed Apr 24, 2018
1 parent 10af92f commit e18f9f4
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 23 deletions.
3 changes: 3 additions & 0 deletions FAILang/FAILangVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ public override IType VisitBinary([NotNull] FAILangParser.BinaryContext context)
case "^":
oper = BinaryOperator.EXPONENT;
break;
case "||":
oper = BinaryOperator.CONCAT;
break;
case "is":
oper = BinaryOperator.IS;
break;
Expand Down
5 changes: 4 additions & 1 deletion FAILang/Grammar/FAILang.g4
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ binary
: prefix
| <assoc=right> binary op=EXPONENT binary
| binary op=( MULTIPLY | DIVIDE ) binary
| binary op=( PLUS | SUBTRACT | PLUS_MINUS ) binary
| binary op=( PLUS | SUBTRACT | PLUS_MINUS | CONCAT ) binary
| binary op=IS binary
;

Expand Down Expand Up @@ -182,6 +182,9 @@ DIVIDE
EXPONENT
: '^'
;
CONCAT
: '||'
;
EQ
: '='
;
Expand Down
3 changes: 3 additions & 0 deletions FAILang/Operator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public enum BinaryOperator
MULTIPLY,
DIVIDE,
EXPONENT,
CONCAT,
IS
}
public enum RelationalOperator
Expand Down Expand Up @@ -54,6 +55,8 @@ public static string ToDisplayString(this BinaryOperator op)
return "/";
case BinaryOperator.EXPONENT:
return "^";
case BinaryOperator.CONCAT:
return "||";
case BinaryOperator.IS:
return "is";
default:
Expand Down
2 changes: 1 addition & 1 deletion FAILang/Types/MathString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public MathString(string value)

public Dictionary<BinaryOperator, Func<IOperable, IType>> BinaryOperators => new Dictionary<BinaryOperator, Func<IOperable, IType>>()
{
{BinaryOperator.ADD, OpConcat}
{BinaryOperator.CONCAT, OpConcat}
};

public Dictionary<RelationalOperator, Func<IOperable, MathBool>> RelativeOperators => null;
Expand Down
15 changes: 14 additions & 1 deletion FAILang/Types/Number.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public Number(Complex value)
{BinaryOperator.PLUS_MINUS, OpPlusMinus},
{BinaryOperator.MULTIPLY, OpMultiply},
{BinaryOperator.DIVIDE, OpDivide},
{BinaryOperator.EXPONENT, OpExponent}
{BinaryOperator.EXPONENT, OpExponent},
{BinaryOperator.CONCAT, OpConcat}
};

public Dictionary<RelationalOperator, Func<IOperable, MathBool>> RelativeOperators => new Dictionary<RelationalOperator, Func<IOperable, MathBool>>() {
Expand Down Expand Up @@ -133,6 +134,18 @@ private IType OpExponent(IOperable other)
return null;
}
}
private IType OpConcat(IOperable other)
{
switch (other)
{
case Number num:
var log = 1 + Complex.Log10(num.value);
Complex floor = new Complex(Math.Floor(log.Real), Math.Floor(log.Imaginary));
return new Number(num.value + (value * Complex.Pow(10, floor)));
default:
return null;
}
}
private MathBool OpEquals(IOperable other)
{
switch (other)
Expand Down
25 changes: 18 additions & 7 deletions FAILang/Types/Tuple.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace FAILang.Types
{
struct Tuple : IType, IIndexable
struct Tuple : IIndexable, IOperable
{
public string TypeName => "Tuple";
public readonly IType[] items;
Expand All @@ -18,18 +18,29 @@ public Tuple(IType[] items)
this.items = items;
}

public IType Index(int index) => items[index];
public Dictionary<BinaryOperator, Func<IOperable, IType>> BinaryOperators => new Dictionary<BinaryOperator, Func<IOperable, IType>>()
{
{BinaryOperator.CONCAT, OpConcat}
};

public IType IndexRange(int left_b, int right_b)
public Dictionary<RelationalOperator, Func<IOperable, MathBool>> RelativeOperators => null;
public Dictionary<UnaryOperator, Func<IType>> UnaryOperators => null;

private IType OpConcat(IOperable other)
{
IType[] newTuple = new IType[right_b - left_b + 1];
for (int i = 0; i <= right_b - left_b; i++)
switch (other)
{
newTuple[i] = items[i + left_b];
case Tuple tup:
return new Tuple(items.Concat(tup.items).ToArray());
default:
return null;
}
return new Tuple(newTuple);
}

public IType Index(int index) => items[index];

public IType IndexRange(int left_b, int right_b) => new Tuple(items.Skip(left_b).SkipLast(Length - right_b - 1).ToArray());

public override string ToString() {
switch (items.Length) {
case 0:
Expand Down
11 changes: 8 additions & 3 deletions FAILang/Types/Unevaluated/IndexerExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public IType Evaluate(Dictionary<string, IType> lookups)
if (!n_left.IsReal || left != n_left.value.Real)
return new Error("IndexError", "Indexer values must be positive integers.");
if (!range && (left < 0 || left >= item.Length))
return new Error("IndexError", $"{left} is out of range.");
return Undefined.instance;
}
IType t_right = rightIndex;
if (rightIndex is IUnevaluated u_right) t_right = u_right.Evaluate(lookups);
Expand All @@ -56,7 +56,7 @@ public IType Evaluate(Dictionary<string, IType> lookups)
if (!n_right.IsReal || right != n_right.value.Real)
return new Error("IndexError", "Indexer values must be positive integers.");
if (!range && (right < 0 || right >= item.Length))
return new Error("IndexError", $"{right} is out of range.");
return Undefined.instance;
}

if (range)
Expand All @@ -66,7 +66,12 @@ public IType Evaluate(Dictionary<string, IType> lookups)
else if (t_left == null && right < left)
left = right;
if (left > right)
return new Error("IndexError", "The first index in an index range cannot be greater than the second index.");
{
var tmp = left;
left = right;
right = tmp;
}

return item.IndexRange(left, right);
}
else
Expand Down
23 changes: 13 additions & 10 deletions FAILang/Types/Vector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public Vector(IType[] items)
public Dictionary<BinaryOperator, Func<IOperable, IType>> BinaryOperators => new Dictionary<BinaryOperator, Func<IOperable, IType>>() {
{BinaryOperator.ADD, OpAdd},
{BinaryOperator.SUBTRACT, OpSubtract},
{BinaryOperator.MULTIPLY, OpMultiply}
{BinaryOperator.MULTIPLY, OpMultiply},
{BinaryOperator.CONCAT, OpConcat}
};

public Dictionary<RelationalOperator, Func<IOperable, MathBool>> RelativeOperators => null;
Expand Down Expand Up @@ -103,6 +104,16 @@ private IType OpMultiply(IOperable other)
return null;
}
}
private IType OpConcat(IOperable other)
{
switch (other)
{
case Vector vec:
return new Vector(items.Concat(vec.items).ToArray());
default:
return null;
}
}
private IType OpNegate()
{
IType[] newItems = new IType[items.Length];
Expand Down Expand Up @@ -132,15 +143,7 @@ private IType OpMagnitude()
}

public IType Index(int index) => items[index];
public IType IndexRange(int left_b, int right_b)
{
IType[] newVector = new IType[right_b - left_b + 1];
for (int i = 0; i <= right_b - left_b; i++)
{
newVector[i] = items[i + left_b];
}
return new Vector(newVector);
}
public IType IndexRange(int left_b, int right_b) => new Vector(items.Skip(left_b).SkipLast(Length - right_b - 1).ToArray());

public override string ToString() => $"<{String.Join(", ", (object[])items)}>";

Expand Down

1 comment on commit e18f9f4

@TheUnlocked
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolves #19

Please sign in to comment.