Skip to content

Commit d8d2e09

Browse files
Version 3.2
1 parent be9892c commit d8d2e09

File tree

7 files changed

+373
-0
lines changed

7 files changed

+373
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//-----------------------------------------------------------------------------
2+
// Copyright (c) 2017-2022 informedcitizenry <[email protected]>
3+
//
4+
// Licensed under the MIT license. See LICENSE for full license information.
5+
//
6+
//-----------------------------------------------------------------------------
7+
8+
using System;
9+
using System.Collections.Generic;
10+
using System.Linq;
11+
12+
namespace Sixty502DotNet
13+
{
14+
public class ConcatFunction : FunctionDefinitionBase
15+
{
16+
public ConcatFunction()
17+
: base("concat", new List<FunctionArg>
18+
{
19+
new FunctionArg("arr1", variantType: true),
20+
new FunctionArg("arr2", variantType: true)
21+
})
22+
{
23+
}
24+
25+
private static Value? ConcatStrings(ArrayValue strVals)
26+
{
27+
if (strVals[0] is Value v1 && v1.IsString &&
28+
strVals[1] is Value v2 && v2.IsString)
29+
{
30+
return new Value($"\"{string.Concat(v1.ToString(true), v2.ToString(true))}\"");
31+
}
32+
throw new Error(Errors.TypeMismatchError);
33+
}
34+
35+
protected override Value? OnInvoke(ArrayValue args)
36+
{
37+
if (args[0] is ArrayValue arr1 && args[1] is ArrayValue arr2 && arr1.TypeCompatible(arr2))
38+
{
39+
var concatlist = arr1.ToList();
40+
concatlist.AddRange(arr2.ToList());
41+
return new ArrayValue(concatlist);
42+
}
43+
return ConcatStrings(args);
44+
}
45+
}
46+
}
47+
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
//-----------------------------------------------------------------------------
2+
// Copyright (c) 2017-2022 informedcitizenry <[email protected]>
3+
//
4+
// Licensed under the MIT license. See LICENSE for full license information.
5+
//
6+
//-----------------------------------------------------------------------------
7+
8+
using System;
9+
using System.Collections.Generic;
10+
using System.Text;
11+
12+
namespace Sixty502DotNet
13+
{
14+
public class FilterFunction : FunctionDefinitionBase
15+
{
16+
public FilterFunction()
17+
: base("filter", new List<FunctionArg>
18+
{
19+
new FunctionArg("array", variantType: true),
20+
new FunctionArg("predicate", TypeCode.Object)
21+
})
22+
{
23+
24+
}
25+
26+
private static Value? FilterString(Value str, FunctionValue predicate)
27+
{
28+
string strVal = str.ToString(true);
29+
StringBuilder filtered = new();
30+
foreach(var c in strVal)
31+
{
32+
ArrayValue args = new()
33+
{
34+
new Value($"'{c}'")
35+
};
36+
Value? match = predicate.Invoke(args);
37+
if (match == null)
38+
{
39+
throw new Error("Predicate did not return a value.");
40+
}
41+
if (match.DotNetType != TypeCode.Boolean)
42+
{
43+
throw new Error(Errors.TypeMismatchError);
44+
}
45+
if (match.ToBool())
46+
{
47+
filtered.Append(c);
48+
}
49+
}
50+
return new Value($"\"{filtered}\"");
51+
}
52+
53+
protected override Value? OnInvoke(ArrayValue args)
54+
{
55+
if (args[1] is FunctionValue predicate)
56+
{
57+
if (args[0].IsString)
58+
{
59+
return FilterString(args[0], predicate);
60+
}
61+
if (args[0] is not ArrayValue arr)
62+
{
63+
throw new Error(Errors.TypeMismatchError);
64+
}
65+
ArrayValue filtered = new();
66+
foreach(var val in arr)
67+
{
68+
ArrayValue args2 = new()
69+
{
70+
val
71+
};
72+
Value? match = predicate.Invoke(args2);
73+
if (match == null)
74+
{
75+
throw new Error("Predicate did not return a value.");
76+
}
77+
if (match.DotNetType != TypeCode.Boolean)
78+
{
79+
throw new Error(Errors.TypeMismatchError);
80+
}
81+
if (match.ToBool())
82+
{
83+
filtered.Add(val);
84+
}
85+
}
86+
return filtered;
87+
}
88+
throw new Error("Parameter must be a function expression.");
89+
}
90+
}
91+
}
92+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//-----------------------------------------------------------------------------
2+
// Copyright (c) 2017-2022 informedcitizenry <[email protected]>
3+
//
4+
// Licensed under the MIT license. See LICENSE for full license information.
5+
//
6+
//-----------------------------------------------------------------------------
7+
8+
using System;
9+
using System.Collections.Generic;
10+
using System.Text;
11+
12+
namespace Sixty502DotNet
13+
{
14+
public class MapFunction : FunctionDefinitionBase
15+
{
16+
public MapFunction()
17+
: base("map", new List<FunctionArg>
18+
{
19+
new FunctionArg("array", TypeCode.Object),
20+
new FunctionArg("transformFunc", TypeCode.Object)
21+
})
22+
{
23+
}
24+
25+
protected override Value? OnInvoke(ArrayValue args)
26+
{
27+
if (args[1] is FunctionValue transformFunc)
28+
{
29+
if (args[0] is not ArrayValue arr)
30+
{
31+
throw new Error(Errors.TypeMismatchError);
32+
}
33+
ArrayValue transformedArray = new ArrayValue();
34+
foreach (var val in arr)
35+
{
36+
ArrayValue args2 = new ArrayValue();
37+
args2.Add(val);
38+
Value? transformed = transformFunc.Invoke(args2);
39+
if (transformed == null)
40+
{
41+
throw new Error("Transform function did not return a value.");
42+
}
43+
if (!transformedArray.TypeCompatible(transformed))
44+
{
45+
throw new Error(Errors.TypeMismatchError);
46+
}
47+
transformedArray.Add(transformed);
48+
}
49+
return transformedArray;
50+
}
51+
throw new Error("Parameter must be a function expression.");
52+
}
53+
}
54+
}
55+
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//-----------------------------------------------------------------------------
2+
// Copyright (c) 2017-2022 informedcitizenry <[email protected]>
3+
//
4+
// Licensed under the MIT license. See LICENSE for full license information.
5+
//
6+
//-----------------------------------------------------------------------------
7+
8+
using System;
9+
using System.Collections.Generic;
10+
11+
namespace Sixty502DotNet
12+
{
13+
public class ReduceFunction : FunctionDefinitionBase
14+
{
15+
public ReduceFunction()
16+
: base("reduce", new List<FunctionArg>
17+
{
18+
new FunctionArg("array", TypeCode.Object),
19+
new FunctionArg("reducer", TypeCode.Object)
20+
})
21+
{
22+
}
23+
24+
protected override Value? OnInvoke(ArrayValue args)
25+
{
26+
if (args[1] is FunctionValue reducer)
27+
{
28+
if (args[0] is ArrayValue array)
29+
{
30+
Value reduced = array[0];
31+
for(int i = 1; i < array.Count; i++)
32+
{
33+
ArrayValue reduceParams = new()
34+
{
35+
reduced,
36+
array[i]
37+
};
38+
Value? reduction = reducer.Invoke(reduceParams);
39+
if (reduction?.IsDefined != true)
40+
{
41+
throw new Error("Function did not return a value.");
42+
}
43+
if (!reduced.SetAs(reduction!))
44+
{
45+
throw new Error(Errors.TypeMismatchError);
46+
}
47+
}
48+
return reduced;
49+
}
50+
throw new Error(Errors.TypeMismatchError);
51+
}
52+
throw new Error("Parameter must be a function expression.");
53+
}
54+
}
55+
}
56+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//-----------------------------------------------------------------------------
2+
// Copyright (c) 2017-2022 informedcitizenry <[email protected]>
3+
//
4+
// Licensed under the MIT license. See LICENSE for full license information.
5+
//
6+
//-----------------------------------------------------------------------------
7+
8+
using System;
9+
using System.Collections.Generic;
10+
using System.Linq;
11+
12+
namespace Sixty502DotNet
13+
{
14+
public class SortFunction : FunctionDefinitionBase
15+
{
16+
public SortFunction()
17+
: base("sort", new List<FunctionArg> { new FunctionArg("array", variantType: true)} )
18+
{
19+
20+
}
21+
22+
protected override Value? OnInvoke(ArrayValue args)
23+
{
24+
if (args[0].IsString)
25+
{
26+
string argStr = args[0].ToString(true);
27+
return new Value($"\"{string.Concat(argStr.OrderBy(c => c))}\"");
28+
}
29+
if (args[0] is ArrayValue arr)
30+
{
31+
if (arr.ElementsNumeric)
32+
{
33+
var numSorted = arr.OrderBy(v => v.ToDouble());
34+
return new ArrayValue(numSorted.ToList());
35+
}
36+
if (arr.ElementType == TypeCode.String)
37+
{
38+
var strSorted = arr.Select(v => v.ToString(true)).ToArray();
39+
Array.Sort(strSorted, StringComparer.Ordinal);
40+
var sortedArr = new ArrayValue();
41+
foreach (var s in strSorted)
42+
sortedArr.Add(new Value($"\"{s}\""));
43+
return sortedArr;
44+
}
45+
if (arr.ElementType == TypeCode.Boolean)
46+
{
47+
var boolSorted = arr.OrderBy(v => v.ToBool());
48+
return new ArrayValue(boolSorted.ToList());
49+
}
50+
}
51+
throw new Error(Errors.TypeMismatchError);
52+
}
53+
}
54+
}
55+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//-----------------------------------------------------------------------------
2+
// Copyright (c) 2017-2022 informedcitizenry <[email protected]>
3+
//
4+
// Licensed under the MIT license. See LICENSE for full license information.
5+
//
6+
//-----------------------------------------------------------------------------
7+
8+
using System;
9+
using System.Collections.Generic;
10+
using System.Linq;
11+
12+
namespace Sixty502DotNet
13+
{
14+
public class UnionFunction : FunctionDefinitionBase
15+
{
16+
public UnionFunction()
17+
: base("union", new List<FunctionArg>
18+
{
19+
new FunctionArg("arr1", TypeCode.Object),
20+
new FunctionArg("arr2", TypeCode.Object)
21+
})
22+
{
23+
}
24+
25+
protected override Value? OnInvoke(ArrayValue args)
26+
{
27+
if (args[0] is ArrayValue arr1 && args[1] is ArrayValue arr2 && arr1.TypeCompatible(arr2))
28+
{
29+
var unionlist = arr1.ToHashSet();
30+
unionlist.UnionWith(arr2.ToHashSet());
31+
return new ArrayValue(unionlist.ToList());
32+
}
33+
throw new NotImplementedException();
34+
}
35+
}
36+
}
37+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//-----------------------------------------------------------------------------
2+
// Copyright (c) 2017-2022 informedcitizenry <[email protected]>
3+
//
4+
// Licensed under the MIT license. See LICENSE for full license information.
5+
//
6+
//-----------------------------------------------------------------------------
7+
8+
namespace Sixty502DotNet
9+
{
10+
public class FunctionValue : Value
11+
{
12+
private readonly UserFunctionDefinition _fcnDefinition;
13+
14+
public FunctionValue(Sixty502DotNetParser.ArrowFuncContext arrowCtx, AssemblyServices services)
15+
{
16+
Visitor = new BlockVisitor(services);
17+
_fcnDefinition = UserFunctionDefinition.Declare(arrowCtx, services.Symbols);
18+
Type = typeof(FunctionValue); // just to get the DotNetType
19+
}
20+
21+
public Value? Invoke(ArrayValue args)
22+
{
23+
_fcnDefinition.Visitor = Visitor;
24+
return _fcnDefinition.Invoke(args);
25+
}
26+
27+
public override string ToString() => "() =>";
28+
29+
public BlockVisitor Visitor { get; init; }
30+
}
31+
}

0 commit comments

Comments
 (0)