Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Const eval for some math procs #1202

Merged
merged 22 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions Content.Tests/DMProject/Tests/Expression/const_eval.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/datum
var/composite_expr = 4 * sin(3 + cos(2))
var/sintest = sin(45)
var/costest = cos(123)
var/tantest = tan(123)
var/sqrttest = sqrt(123)
var/arcsintest = arcsin(sin(45))
var/arccostest = arccos(cos(123))
var/arctantest = arctan(tan(69))
var/log_test = log(10)
var/log10_test = log(10, 100)
var/arctan2_test = arctan(1, 3)
var/abs_test = abs(-213)

#define EPSILON 4e-6
#define APX_EQUAL(a, b) ASSERT(abs(a - b) < EPSILON)

/proc/RunTest()
var/break_const_eval = null
var/datum/d = new /datum

APX_EQUAL(initial(d.composite_expr), 4 * sin(3 + cos(break_const_eval || 2)))
APX_EQUAL(d.composite_expr, 4 * sin(3 + cos(break_const_eval || 2)))

APX_EQUAL(d.sintest, sin(break_const_eval || 45))
APX_EQUAL(d.sintest, 0.707106769084930419921875)

APX_EQUAL(d.costest, cos(break_const_eval || 123))
APX_EQUAL(d.costest, -0.544639050960540771484375)

APX_EQUAL(d.tantest, tan(break_const_eval || 123))
APX_EQUAL(d.tantest, -1.539865016937255859375)

APX_EQUAL(d.sqrttest, sqrt(break_const_eval || 123))
APX_EQUAL(d.sqrttest, 11.0905361175537109375)

APX_EQUAL(d.arcsintest, arcsin(sin(break_const_eval || 45)))
APX_EQUAL(d.arcsintest, 45)

APX_EQUAL(d.arccostest, arccos(cos(break_const_eval || 123)))
APX_EQUAL(d.arccostest, 123)

APX_EQUAL(d.arctantest, arctan(tan(break_const_eval || 69)))
APX_EQUAL(d.arctantest, 69)

APX_EQUAL(d.log_test, log(break_const_eval || 10))
APX_EQUAL(d.log_test, 2.302585124969482421875)

APX_EQUAL(d.log10_test, log(break_const_eval || 10, 100))
APX_EQUAL(d.log10_test, 2)

APX_EQUAL(d.arctan2_test, arctan(break_const_eval || 1, 3))
APX_EQUAL(d.arctan2_test, 71.5650482177734375)

APX_EQUAL(d.abs_test, abs(break_const_eval || -213))

13 changes: 12 additions & 1 deletion DMCompiler/Bytecode/DreamProcOpcode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,18 @@ public enum DreamProcOpcode : byte {
GetStep = 0x75,
Length = 0x76,
GetDir = 0x77,
DebuggerBreakpoint = 0x78
DebuggerBreakpoint = 0x78,
Sin = 0x79,
Cos = 0x7A,
Tan = 0x7B,
Arcsin = 0x7C,
Arccos = 0x7D,
Arctan = 0x7E,
Arctan2 = 0x7F,
Sqrt = 0x80,
Log = 0x81,
LogE = 0x82,
Abs = 0x83,
}

/// <summary>
Expand Down
155 changes: 154 additions & 1 deletion DMCompiler/Compiler/DM/DMAST.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using JetBrains.Annotations;
using System.Linq;
using OpenDreamShared.Compiler;
using OpenDreamShared.Dream;
Expand Down Expand Up @@ -262,7 +263,36 @@ public void VisitGradient(DMASTGradient gradient) {
public void VisitPick(DMASTPick pick) {
throw new NotImplementedException();
}

public void VisitSin(DMASTSin sin) {
throw new NotImplementedException();
}
public void VisitCos(DMASTCos cos) {
throw new NotImplementedException();
}
public void VisitTan(DMASTTan tan) {
throw new NotImplementedException();
}
public void VisitArcsin(DMASTArcsin asin) {
throw new NotImplementedException();
}
public void VisitArccos(DMASTArccos acos) {
throw new NotImplementedException();
}
public void VisitArctan(DMASTArctan atan) {
throw new NotImplementedException();
}
public void VisitArctan2(DMASTArctan2 atan) {
throw new NotImplementedException();
}
public void VisitSqrt(DMASTSqrt sqrt) {
throw new NotImplementedException();
}
public void VisitLog(DMASTLog log) {
throw new NotImplementedException();
}
public void VisitAbs(DMASTAbs abs) {
throw new NotImplementedException();
}
public void VisitCall(DMASTCall call) {
throw new NotImplementedException();
}
Expand Down Expand Up @@ -1554,6 +1584,129 @@ public override void Visit(DMASTVisitor visitor) {
}
}

public class DMASTSin : DMASTExpression {
public DMASTExpression Expression;

public DMASTSin(Location location, DMASTExpression expression) : base(location) {
Expression = expression;
}

public override void Visit(DMASTVisitor visitor) {
visitor.VisitSin(this);
}
}

public class DMASTCos : DMASTExpression {
public DMASTExpression Expression;

public DMASTCos(Location location, DMASTExpression expression) : base(location) {
Expression = expression;
}

public override void Visit(DMASTVisitor visitor) {
visitor.VisitCos(this);
}
}

public class DMASTTan : DMASTExpression {
public DMASTExpression Expression;

public DMASTTan(Location location, DMASTExpression expression) : base(location) {
Expression = expression;
}

public override void Visit(DMASTVisitor visitor) {
visitor.VisitTan(this);
}
}

public class DMASTArcsin : DMASTExpression {
public DMASTExpression Expression;

public DMASTArcsin(Location location, DMASTExpression expression) : base(location) {
Expression = expression;
}

public override void Visit(DMASTVisitor visitor) {
visitor.VisitArcsin(this);
}
}

public class DMASTArccos : DMASTExpression {
public DMASTExpression Expression;

public DMASTArccos(Location location, DMASTExpression expression) : base(location) {
Expression = expression;
}

public override void Visit(DMASTVisitor visitor) {
visitor.VisitArccos(this);
}
}

public class DMASTArctan : DMASTExpression {
public DMASTExpression Expression;

public DMASTArctan(Location location, DMASTExpression expression) : base(location) {
Expression = expression;
}

public override void Visit(DMASTVisitor visitor) {
visitor.VisitArctan(this);
}
}

public class DMASTArctan2 : DMASTExpression {
public DMASTExpression XExpression;
public DMASTExpression YExpression;

public DMASTArctan2(Location location, DMASTExpression xExpression, DMASTExpression yExpression) : base(location) {
XExpression = xExpression;
YExpression = yExpression;
}

public override void Visit(DMASTVisitor visitor) {
visitor.VisitArctan2(this);
}
}

public class DMASTSqrt : DMASTExpression {
public DMASTExpression Expression;

public DMASTSqrt(Location location, DMASTExpression expression) : base(location) {
Expression = expression;
}

public override void Visit(DMASTVisitor visitor) {
visitor.VisitSqrt(this);
}
}

public class DMASTLog : DMASTExpression {
public DMASTExpression Expression;
public DMASTExpression? BaseExpression;

public DMASTLog(Location location, DMASTExpression expression, DMASTExpression? baseExpression) : base(location) {
Expression = expression;
BaseExpression = baseExpression;
}

public override void Visit(DMASTVisitor visitor) {
visitor.VisitLog(this);
}
}

public class DMASTAbs : DMASTExpression {
public DMASTExpression Expression;

public DMASTAbs(Location location, DMASTExpression expression) : base(location) {
Expression = expression;
}

public override void Visit(DMASTVisitor visitor) {
visitor.VisitAbs(this);
}
}
public sealed class DMASTCall : DMASTExpression {
public readonly DMASTCallParameter[] CallParameters, ProcParameters;

Expand Down
49 changes: 49 additions & 0 deletions DMCompiler/Compiler/DM/DMParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2412,6 +2412,55 @@ private DMASTExpression ParseProcCall(DMASTExpression expression) {

return new DMASTIsSaved(identifier.Location, callParameters[0].Value);
}
case "sin": {
if (callParameters.Length != 1) Error("sin() requires 1 argument");

return new DMASTSin(identifier.Location, callParameters[0].Value);
}
case "cos": {
if (callParameters.Length != 1) Error("cos() requires 1 argument");

return new DMASTCos(identifier.Location, callParameters[0].Value);
}
case "tan": {
if (callParameters.Length != 1) Error("tan() requires 1 argument");

return new DMASTTan(identifier.Location, callParameters[0].Value);
}
case "arcsin": {
if (callParameters.Length != 1) Error("arcsin() requires 1 argument");

return new DMASTArcsin(identifier.Location, callParameters[0].Value);
}
case "arccos": {
if (callParameters.Length != 1) Error("arccos() requires 1 argument");

return new DMASTArccos(identifier.Location, callParameters[0].Value);
}
case "arctan": {
if (callParameters.Length != 1 && callParameters.Length != 2)
Error("arctan() requires 1 or 2 arguments");
if (callParameters.Length == 1)
return new DMASTArctan(identifier.Location, callParameters[0].Value);
return new DMASTArctan2(identifier.Location, callParameters[0].Value, callParameters[1].Value);
}
case "sqrt": {
if (callParameters.Length != 1) Error("sqrt() requires 1 argument");

return new DMASTSqrt(identifier.Location, callParameters[0].Value);
}
case "log": {
if (callParameters.Length != 1 && callParameters.Length != 2)
Error("log() requires 1 or 2 arguments");
if (callParameters.Length == 1)
return new DMASTLog(identifier.Location, callParameters[0].Value, null);
return new DMASTLog(identifier.Location, callParameters[1].Value, callParameters[0].Value);
}
case "abs": {
if (callParameters.Length != 1) Error("abs() requires 1 argument");

return new DMASTAbs(identifier.Location, callParameters[0].Value);
}
case "istype": {
if (callParameters.Length == 1) {
return new DMASTImplicitIsType(identifier.Location, callParameters[0].Value);
Expand Down
46 changes: 46 additions & 0 deletions DMCompiler/DM/DMProc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,52 @@ public void LessThanOrEqual() {
WriteOpcode(DreamProcOpcode.CompareLessThanOrEqual);
}

public void Sin() {
WriteOpcode(DreamProcOpcode.Sin);
}

public void Cos() {
WriteOpcode(DreamProcOpcode.Cos);
}

public void Tan() {
WriteOpcode(DreamProcOpcode.Tan);
}

public void Arcsin() {
WriteOpcode(DreamProcOpcode.Arcsin);
}

public void Arccos() {
WriteOpcode(DreamProcOpcode.Arccos);
}

public void Arctan() {
WriteOpcode(DreamProcOpcode.Arctan);
}

public void Arctan2() {
ShrinkStack(1);
WriteOpcode(DreamProcOpcode.Arctan2);
}

public void Sqrt() {
WriteOpcode(DreamProcOpcode.Sqrt);
}

public void Log() {
ShrinkStack(1);
WriteOpcode(DreamProcOpcode.Log);
}

public void LogE() {
WriteOpcode(DreamProcOpcode.LogE);
}

public void Abs() {
WriteOpcode(DreamProcOpcode.Abs);
}

public void PushFloat(float value) {
GrowStack(1);
WriteOpcode(DreamProcOpcode.PushFloat);
Expand Down
Loading
Loading