Skip to content

Commit

Permalink
#96 change lambda expr syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
shapoco committed Dec 23, 2023
1 parent 2b33bc5 commit 0e3becc
Show file tree
Hide file tree
Showing 10 changed files with 257 additions and 158 deletions.
4 changes: 3 additions & 1 deletion Calctus.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@
<Compile Include="Calctus\Model\Evaluations\EvalError.cs" />
<Compile Include="Calctus\Model\Evaluations\ValComparer.cs" />
<Compile Include="Calctus\Model\Evaluations\ValEqualityComparer.cs" />
<Compile Include="Calctus\Model\Expressions\AsterExpr.cs" />
<Compile Include="Calctus\Model\Expressions\LambdaExpr.cs" />
<Compile Include="Calctus\Model\Expressions\ParenthesisExpr.cs" />
<Compile Include="Calctus\Model\Formats\FormatSettingss.cs" />
<Compile Include="Calctus\Model\Functions\ArgDefList.cs" />
<Compile Include="Calctus\Model\Functions\EqualityComparerFunc.cs" />
Expand Down Expand Up @@ -223,7 +225,7 @@
<Compile Include="Calctus\Model\Expressions\Operator.cs" />
<Compile Include="Calctus\Model\Expressions\PartRef.cs" />
<Compile Include="Calctus\Model\Expressions\UnaryOp.cs" />
<Compile Include="Calctus\Model\Expressions\VarRef.cs" />
<Compile Include="Calctus\Model\Expressions\IdExpr.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Windows\StartupShortcut.cs" />
<EmbeddedResource Include="Calctus\UI\ConstEditForm.resx">
Expand Down
2 changes: 1 addition & 1 deletion Calctus/Model/Evaluations/EvalContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public IEnumerable<FuncDef> AllNamedFuncs() =>
EmbeddedFuncDef.NativeFunctions.Select(p => (FuncDef)p).Concat(ExternalFuncDef.ExternalFunctions).Concat(_userFuncs);

public bool SolveFunc(string name , out FuncDef func) {
func = AllNamedFuncs().First(p => p.Name != null && p.Name.Text == name);
func = AllNamedFuncs().FirstOrDefault(p => p.Name != null && p.Name.Text == name);
return func != null;
}

Expand Down
24 changes: 24 additions & 0 deletions Calctus/Model/Expressions/AsterExpr.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Shapoco.Calctus.Model.Evaluations;
using Shapoco.Calctus.Model.Types;
using Shapoco.Calctus.Model.Parsers;
using Shapoco.Calctus.Model.Functions;

namespace Shapoco.Calctus.Model.Expressions {
class AsterExpr : Expr{
public readonly IdExpr Id;
public AsterExpr(Token t, IdExpr id) : base(t) {
Id = id;
}

public override bool CausesValueChange() => true;

protected override Val OnEval(EvalContext e) {
throw new EvalError(e, Token, "Unexpected asterisk.");
}
}
}
8 changes: 4 additions & 4 deletions Calctus/Model/Expressions/BinaryOp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@ public BinaryOp(Token t, Expr a, Expr b) : base(OpDef.Match(OpType.Binary, t), t

protected override Val OnEval(EvalContext e) {
if (Method == OpDef.Assign) {
if (A is VarRef aRef) {
if (A is IdExpr aRef) {
// 変数の参照
var val = B.Eval(e);
e.Ref(aRef.RefName, allowCreate: true).Value = val;
e.Ref(aRef.Id, allowCreate: true).Value = val;
return val;
}
else if (A is PartRef pRef && pRef.Target is VarRef tRef) {
else if (A is PartRef pRef && pRef.Target is IdExpr tRef) {
// Part Select を使った参照
var from = pRef.IndexFrom.Eval(e).AsInt;
var to = pRef.IndexTo.Eval(e).AsInt;
if (from < to) throw new ArgumentOutOfRangeException();

var val = B.Eval(e);
var varRef = e.Ref(tRef.RefName, allowCreate: false);
var varRef = e.Ref(tRef.Id, allowCreate: false);
var varVal = varRef.Value;
if (varVal is ArrayVal array) {
// 配列の書き換え
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
using Shapoco.Calctus.Model.Functions;

namespace Shapoco.Calctus.Model.Expressions {
class VarRef : Expr {
public Token RefName => Token;
public VarRef(Token name) : base(name) { }
class IdExpr : Expr {
public Token Id => Token;
public IdExpr(Token id) : base(id) { }

public override bool CausesValueChange() => true;

protected override Val OnEval(EvalContext ctx) {
if (ctx.Ref(RefName, false, out Var v)) {
if (ctx.Ref(Id, false, out Var v)) {
return v.Value;
}
else if (ctx.SolveFunc(RefName.Text, out FuncDef f)) {
else if (ctx.SolveFunc(Id.Text, out FuncDef f)) {
return new FuncVal(f);
}
else {
throw new EvalError(ctx, RefName, "variant or function not found.");
throw new EvalError(ctx, Id, "Variant or function '" + Id.Text + "' not found.");
}
}
}
Expand Down
35 changes: 35 additions & 0 deletions Calctus/Model/Expressions/ParenthesisExpr.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Shapoco.Calctus.Model.Evaluations;
using Shapoco.Calctus.Model.Parsers;
using Shapoco.Calctus.Model.Types;
using Shapoco.Calctus.Model.Functions;

namespace Shapoco.Calctus.Model.Expressions {
class ParenthesisExpr : Expr {
public readonly Expr[] Exprs;
public readonly Token Comma;

public override bool CausesValueChange() => Exprs.Any(p => p.CausesValueChange());

public ParenthesisExpr(Token token, Token firstComma, Expr[] exprs) : base(token) {
this.Exprs = exprs;
this.Comma = firstComma;
}

protected override Val OnEval(EvalContext e) {
if (Exprs.Length == 0) {
throw new EvalError(e, Token, "Empty expression");
}
else if (Exprs.Length >=2) {
throw new EvalError(e, Comma, "')' is expected.");
}
else {
return Exprs[0].Eval(e);
}
}
}
}
Loading

0 comments on commit 0e3becc

Please sign in to comment.