Skip to content

Commit

Permalink
Merge pull request #55 from metaindu/evaluator-24-re-org
Browse files Browse the repository at this point in the history
Evaluator 24: Re-org
  • Loading branch information
izrik authored Oct 21, 2022
2 parents 2ec30f1 + ff30525 commit 88c0453
Show file tree
Hide file tree
Showing 61 changed files with 3,207 additions and 1,259 deletions.
107 changes: 107 additions & 0 deletions Compiler/ILCompiler.Expressions.ComponentAccess.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@

/*
* MetaphysicsIndustries.Solus
* Copyright (C) 2006-2021 Metaphysics Industries, Inc., Richard Sartor
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*
*/

using System;
using MetaphysicsIndustries.Solus.Compiler.IlExpressions;
using MetaphysicsIndustries.Solus.Exceptions;
using MetaphysicsIndustries.Solus.Expressions;

namespace MetaphysicsIndustries.Solus.Compiler
{
public partial class ILCompiler
{
static Type[] GetTypeArrayOfInt(int length)
{
var a = new Type[length];
int i;
for (i = 0; i < length; i++)
a[i] = typeof(int);
return a;
}

public IlExpression ConvertToIlExpression(ComponentAccess expr,
NascentMethod nm)
{
var expr2 = ConvertToIlExpression(expr.Expr, nm);
var indexes2 = new IlExpression[expr.Indexes.Count];
int i;
for (i = 0; i < indexes2.Length; i++)
{
indexes2[i] = new ConvertI4IlExpression(
ConvertToIlExpression(expr.Indexes[i], nm));
}

// TODO: check expr.ResultType against the number of indexes
// eventually, we will have a type system that can tel us
// exactly what we should expect (real number, integer,
// vector on R^3, etc.) at any point in the computation.
// Until then, we use approximations.
if (expr2.ResultType == typeof(string))
{
if (expr.Indexes.Count != 1)
throw new OperandException(
"Wrong number of indexes for the expression");
var charType = typeof(char);
var toString = charType.GetMethod("ToString",
Type.EmptyTypes);
var strType = typeof(string);
var getChars = strType.GetMethod("get_Chars",
new[] { typeof(int) });
var local = nm.CreateLocal();
local.Usage = IlLocalUsage.Internal;
local.LocalType = typeof(char);

return new IlExpressionSequence(
new StoreLocalIlExpression(
local,
new CallIlExpression(
getChars,
expr2, indexes2[0])),
new CallIlExpression(
toString,
new LoadLocalAddrIlExpression(local)));
}

// assume vector (and not string) for now
if (expr.Indexes.Count == 1)
return new LoadElemIlExpression(expr2, indexes2[0]);

// higher rank tensor
if (expr.Indexes.Count >= 2)
{
var arrayType = typeof(float).MakeArrayType(
expr.Indexes.Count);
var getMethod = arrayType.GetMethod("Get",
GetTypeArrayOfInt(expr.Indexes.Count));
var args = new IlExpression[indexes2.Length + 1];
args[0] = expr2;
indexes2.CopyTo(args, 1);
var callExpr = new CallIlExpression(getMethod, args);
return callExpr;
}

// TODO: string?

throw new NotImplementedException();
}
}
}
45 changes: 45 additions & 0 deletions Compiler/ILCompiler.Expressions.FunctionCall.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

/*
* MetaphysicsIndustries.Solus
* Copyright (C) 2006-2021 Metaphysics Industries, Inc., Richard Sartor
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*
*/

using System;
using MetaphysicsIndustries.Solus.Compiler.IlExpressions;
using MetaphysicsIndustries.Solus.Expressions;
using MetaphysicsIndustries.Solus.Functions;

namespace MetaphysicsIndustries.Solus.Compiler
{
public partial class ILCompiler
{
public IlExpression ConvertToIlExpression(
FunctionCall expr, NascentMethod nm)
{
if (expr.Function is Literal literal &&
literal.Value is Function f)
return ConvertToIlExpression(f, nm, expr.Arguments);

// TODO:
throw new NotImplementedException(
"What should be done? Should the expression be " +
"evaluated? Compiled?");
}
}
}
101 changes: 101 additions & 0 deletions Compiler/ILCompiler.Expressions.Literal.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@

/*
* MetaphysicsIndustries.Solus
* Copyright (C) 2006-2021 Metaphysics Industries, Inc., Richard Sartor
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*
*/

using System;
using System.Collections.Generic;
using MetaphysicsIndustries.Solus.Compiler.IlExpressions;
using MetaphysicsIndustries.Solus.Expressions;

namespace MetaphysicsIndustries.Solus.Compiler
{
public partial class ILCompiler
{
public IlExpression ConvertToIlExpression(
Literal expr, NascentMethod nm)
{
if (expr.Value.IsIsScalar(null))
{
var value = expr.Value.ToFloat();
return new LoadConstantIlExpression(value);
}

if (expr.Value.IsIsVector(null))
{
var v = expr.Value.ToVector();
var seq = new List<IlExpression>();
var newarr = new NewArrIlExpression(
typeof(float),
new LoadConstantIlExpression(v.Length));
seq.Add(newarr);
int i;
// for (i = 0; i < v.Length; i++)
// seq.Add();
for (i = 0; i < v.Length; i++)
{
seq.Add(
new StoreElemIlExpression(
array_: new DupIlExpression(newarr),
index: new LoadConstantIlExpression(i),
value: new LoadConstantIlExpression(
v[i].ToNumber().Value)));
}

return new IlExpressionSequence(seq);
}

if (expr.Value.IsIsMatrix(null))
{
var m = expr.Value.ToMatrix();
var arrayType = typeof(float[,]);
var ctor = arrayType.GetConstructor(
new[] { typeof(int), typeof(int) });
var setMethod = arrayType.GetMethod("Set",
new[] { typeof(int), typeof(int), typeof(float) });
var seq = new List<IlExpression>();
seq.Add(new NewObjIlExpression(ctor,
new LoadConstantIlExpression(m.RowCount),
new LoadConstantIlExpression(m.ColumnCount)));
var dup = new DupIlExpression();
int r, c;
for (r = 0; r < m.RowCount; r++)
for (c = 0; c < m.ColumnCount; c++)
seq.Add(
new CallIlExpression(
setMethod,
dup,
new LoadConstantIlExpression(r),
new LoadConstantIlExpression(c),
new LoadConstantIlExpression(m[r, c].ToFloat())));

return new IlExpressionSequence(seq);
}

if (expr.Value.IsIsString(null))
return new LoadStringIlExpression(
expr.Value.ToStringValue().Value);

throw new NotImplementedException(
"currently only implemented for numbers, vectors, " +
" matrices, and strings.");
}
}
}
58 changes: 58 additions & 0 deletions Compiler/ILCompiler.Expressions.MatrixExpression.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

/*
* MetaphysicsIndustries.Solus
* Copyright (C) 2006-2021 Metaphysics Industries, Inc., Richard Sartor
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*
*/

using System.Collections.Generic;
using MetaphysicsIndustries.Solus.Compiler.IlExpressions;
using MetaphysicsIndustries.Solus.Expressions;

namespace MetaphysicsIndustries.Solus.Compiler
{
public partial class ILCompiler
{
public IlExpression ConvertToIlExpression(MatrixExpression expr,
NascentMethod nm)
{
var arrayType = typeof(float[,]);
var ctor = arrayType.GetConstructor(
new[] { typeof(int), typeof(int) });
var setMethod = arrayType.GetMethod("Set",
new[] { typeof(int), typeof(int), typeof(float) });
var seq = new List<IlExpression>();
var newobj = new NewObjIlExpression(ctor,
new LoadConstantIlExpression(expr.RowCount),
new LoadConstantIlExpression(expr.ColumnCount));
seq.Add(newobj);
var dup = new DupIlExpression(newobj);
int r, c;
for (r = 0; r < expr.RowCount; r++)
for (c = 0; c < expr.ColumnCount; c++)
seq.Add(
new CallIlExpression(
setMethod,
dup,
new LoadConstantIlExpression(r),
new LoadConstantIlExpression(c),
ConvertToIlExpression(expr[r, c], nm)));
return new IlExpressionSequence(seq);
}
}
}
39 changes: 39 additions & 0 deletions Compiler/ILCompiler.Expressions.VariableAccess.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

/*
* MetaphysicsIndustries.Solus
* Copyright (C) 2006-2021 Metaphysics Industries, Inc., Richard Sartor
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*
*/

using MetaphysicsIndustries.Solus.Compiler.IlExpressions;
using MetaphysicsIndustries.Solus.Expressions;

namespace MetaphysicsIndustries.Solus.Compiler
{
public partial class ILCompiler
{
public IlExpression ConvertToIlExpression(
VariableAccess expr, NascentMethod nm)
{
var index =
nm.CreateIndexOfLocalForVariableName(expr.VariableName);
var local = nm.Locals[index];
return new LoadLocalIlExpression(local);
}
}
}
Loading

0 comments on commit 88c0453

Please sign in to comment.