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

Refactor common code #2226

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion Content.Tests/DummyDreamMapManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using DMCompiler.Json;
using OpenDreamShared.Common.Json;
using OpenDreamRuntime.Map;
using OpenDreamRuntime.Objects;
using OpenDreamRuntime.Objects.Types;
Expand Down
2 changes: 1 addition & 1 deletion Content.Tests/RuntimeTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using DMCompiler.Bytecode;
using NUnit.Framework;
using OpenDreamShared.Common.Bytecode;

namespace Content.Tests;

Expand Down
2 changes: 1 addition & 1 deletion DMCompiler/Compiler/DM/AST/DMAST.Expression.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Linq;
using DMCompiler.DM;
using OpenDreamShared.Common.DM;

namespace DMCompiler.Compiler.DM.AST;

Expand Down
1 change: 1 addition & 0 deletions DMCompiler/Compiler/DM/AST/DMAST.ObjectStatements.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DMCompiler.DM;
using OpenDreamShared.Common;

namespace DMCompiler.Compiler.DM.AST;

Expand Down
1 change: 1 addition & 0 deletions DMCompiler/Compiler/DM/AST/DMAST.ProcStatements.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DMCompiler.DM;
using OpenDreamShared.Common;

namespace DMCompiler.Compiler.DM.AST;

Expand Down
1 change: 1 addition & 0 deletions DMCompiler/Compiler/DM/AST/DMAST.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DMCompiler.DM;
using OpenDreamShared.Common;

namespace DMCompiler.Compiler.DM.AST;

Expand Down
2 changes: 2 additions & 0 deletions DMCompiler/Compiler/DM/DMParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System.Linq;
using DMCompiler.Compiler.DM.AST;
using DMCompiler.DM;
using OpenDreamShared.Common;
using OpenDreamShared.Common.DM;

namespace DMCompiler.Compiler.DM {
internal partial class DMParser(DMCompiler compiler, DMLexer lexer) : Parser<Token>(compiler, lexer) {
Expand Down
2 changes: 1 addition & 1 deletion DMCompiler/Compiler/DM/DMParserHelper.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using DMCompiler.Bytecode;
using DMCompiler.Compiler.DM.AST;
using OpenDreamShared.Common.Bytecode;

namespace DMCompiler.Compiler.DM;

Expand Down
4 changes: 3 additions & 1 deletion DMCompiler/Compiler/DM/DMPath.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace DMCompiler.Compiler.DM;
using OpenDreamShared.Common;

namespace DMCompiler.Compiler.DM;

internal abstract class VarDeclInfo {
public DreamPath? TypePath;
Expand Down
3 changes: 2 additions & 1 deletion DMCompiler/Compiler/DMM/DMMParser.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using DMCompiler.Compiler.DM;
using DMCompiler.Compiler.DM.AST;
using DMCompiler.DM.Builders;
using DMCompiler.Json;
using OpenDreamShared.Common;
using OpenDreamShared.Common.Json;

namespace DMCompiler.Compiler.DMM;

Expand Down
1 change: 1 addition & 0 deletions DMCompiler/DM/Builders/DMCodeTreeBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DMCompiler.Compiler.DM.AST;
using OpenDreamShared.Common;

namespace DMCompiler.DM.Builders;

Expand Down
2 changes: 2 additions & 0 deletions DMCompiler/DM/Builders/DMExpressionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using DMCompiler.DM.Expressions;
using static DMCompiler.DM.Builders.DMExpressionBuilder.ScopeMode;
using String = DMCompiler.DM.Expressions.String;
using OpenDreamShared.Common;
using OpenDreamShared.Common.DM;

namespace DMCompiler.DM.Builders;

Expand Down
4 changes: 3 additions & 1 deletion DMCompiler/DM/Builders/DMProcBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System.Diagnostics;
using DMCompiler.Bytecode;
using DMCompiler.Compiler;
using DMCompiler.Compiler.DM;
using DMCompiler.Compiler.DM.AST;
using DMCompiler.DM.Expressions;
using OpenDreamShared.Common;
using OpenDreamShared.Common.Bytecode;
using OpenDreamShared.Common.DM;

namespace DMCompiler.DM.Builders {
internal sealed class DMProcBuilder(DMCompiler compiler, DMObject dmObject, DMProc proc) {
Expand Down
1 change: 1 addition & 0 deletions DMCompiler/DM/DMCodeTree.Procs.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using DMCompiler.Compiler;
using DMCompiler.Compiler.DM.AST;
using OpenDreamShared.Common;

namespace DMCompiler.DM;

Expand Down
4 changes: 3 additions & 1 deletion DMCompiler/DM/DMCodeTree.Vars.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System.Diagnostics.CodeAnalysis;
using DMCompiler.Bytecode;
using DMCompiler.Compiler;
using DMCompiler.Compiler.DM.AST;
using DMCompiler.DM.Builders;
using DMCompiler.DM.Expressions;
using OpenDreamShared.Common;
using OpenDreamShared.Common.Bytecode;
using OpenDreamShared.Common.DM;
using ScopeMode = DMCompiler.DM.Builders.DMExpressionBuilder.ScopeMode;

namespace DMCompiler.DM;
Expand Down
1 change: 1 addition & 0 deletions DMCompiler/DM/DMCodeTree.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Diagnostics.CodeAnalysis;
using DMCompiler.Compiler;
using DMCompiler.DM.Builders;
using OpenDreamShared.Common;

namespace DMCompiler.DM;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,7 @@
namespace DMCompiler.DM;
using OpenDreamShared.Common;
using OpenDreamShared.Common.DM;

// If you are modifying this, you must also modify OpenDreamShared.Dream.DreamValueType !!
// Unfortunately the client needs this and it can't reference DMCompiler due to the sandbox

///<summary>
///Stores any explicit casting done via the "as" keyword. Also stores compiler hints for DMStandard.<br/>
///is a [Flags] enum because it's possible for something to have multiple values (especially with the quirky DMStandard ones)
/// </summary>
[Flags]
public enum DMValueType {
Anything = 0x0,
Null = 0x1,
Text = 0x2,
Obj = 0x4,
Mob = 0x8,
Turf = 0x10,
Num = 0x20,
Message = 0x40,
Area = 0x80,
Color = 0x100,
File = 0x200,
CommandText = 0x400,
Sound = 0x800,
Icon = 0x1000,
Path = 0x2000, // For proc return types

//Byond here be dragons
Unimplemented = 0x4000, // Marks that a method or property is not implemented. Throws a compiler warning if accessed.
CompiletimeReadonly = 0x8000, // Marks that a property can only ever be read from, never written to. This is a const-ier version of const, for certain standard values like list.type
NoConstFold = 0x10000 // Marks that a const var cannot be const-folded during compile
}
namespace DMCompiler.DM;

/// <summary>
/// Allows for more complex things than DMValueType does, such as supporting type paths
Expand Down
4 changes: 3 additions & 1 deletion DMCompiler/DM/DMExpression.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using DMCompiler.Bytecode;
using System.Diagnostics.CodeAnalysis;
using DMCompiler.Compiler;
using DMCompiler.DM.Expressions;
using OpenDreamShared.Common;
using OpenDreamShared.Common.Bytecode;
using OpenDreamShared.Common.DM;

namespace DMCompiler.DM;

Expand Down
6 changes: 4 additions & 2 deletions DMCompiler/DM/DMObject.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.Linq;
using DMCompiler.Bytecode;
using DMCompiler.DM.Expressions;
using DMCompiler.Json;
using OpenDreamShared.Common;
using OpenDreamShared.Common.Bytecode;
using OpenDreamShared.Common.DM;
using OpenDreamShared.Common.Json;

namespace DMCompiler.DM;

Expand Down
3 changes: 2 additions & 1 deletion DMCompiler/DM/DMObjectTree.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using System.Diagnostics.CodeAnalysis;
using DMCompiler.Compiler;
using DMCompiler.Compiler.DM.AST;
using DMCompiler.Json;
using OpenDreamShared.Common;
using OpenDreamShared.Common.Json;

namespace DMCompiler.DM;

Expand Down
6 changes: 4 additions & 2 deletions DMCompiler/DM/DMProc.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using DMCompiler.Bytecode;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using DMCompiler.DM.Expressions;
using DMCompiler.Compiler;
using DMCompiler.Compiler.DM.AST;
using DMCompiler.DM.Builders;
using DMCompiler.Json;
using DMCompiler.Optimizer;
using OpenDreamShared.Common.Bytecode;
using OpenDreamShared.Common.DM;
using OpenDreamShared.Common;
using OpenDreamShared.Common.Json;

namespace DMCompiler.DM {
internal sealed class DMProc {
Expand Down
4 changes: 3 additions & 1 deletion DMCompiler/DM/DMVariable.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Diagnostics.CodeAnalysis;
using OpenDreamShared.Common;
using OpenDreamShared.Common.DM;
using System.Diagnostics.CodeAnalysis;

namespace DMCompiler.DM;

Expand Down
3 changes: 2 additions & 1 deletion DMCompiler/DM/Expressions/Binary.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Diagnostics.CodeAnalysis;
using DMCompiler.Bytecode;
using DMCompiler.Compiler;
using OpenDreamShared.Common;
using OpenDreamShared.Common.Bytecode;

namespace DMCompiler.DM.Expressions;

Expand Down
10 changes: 6 additions & 4 deletions DMCompiler/DM/Expressions/Builtins.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using DMCompiler.Bytecode;
using System.Diagnostics.CodeAnalysis;
using DMCompiler.Compiler;
using DMCompiler.Json;
using OpenDreamShared.Common;
using OpenDreamShared.Common.Bytecode;
using OpenDreamShared.Common.DM;
using OpenDreamShared.Common.Json;

namespace DMCompiler.DM.Expressions;

Expand Down Expand Up @@ -61,7 +63,7 @@ public void EmitPushArglist(ExpressionContext ctx) {
internal sealed class New(DMCompiler compiler, Location location, DMExpression expr, ArgumentList arguments) : DMExpression(location) {
public override DreamPath? Path => expr.Path;
public override bool PathIsFuzzy => Path == null;
public override DMComplexValueType ValType => !expr.ValType.IsAnything ? expr.ValType : (Path?.GetAtomType(compiler) ?? DMValueType.Anything);
public override DMComplexValueType ValType => !expr.ValType.IsAnything ? expr.ValType : (compiler.GetAtomType(Path) ?? DMValueType.Anything);

public override void EmitPushValue(ExpressionContext ctx) {
var argumentInfo = arguments.EmitArguments(ctx, null);
Expand All @@ -74,7 +76,7 @@ public override void EmitPushValue(ExpressionContext ctx) {
// new /x/y/z (...)
internal sealed class NewPath(DMCompiler compiler, Location location, IConstantPath create, ArgumentList arguments) : DMExpression(location) {
public override DreamPath? Path => (create is ConstantTypeReference typeReference) ? typeReference.Path : null;
public override DMComplexValueType ValType => Path?.GetAtomType(compiler) ?? DMValueType.Anything;
public override DMComplexValueType ValType => compiler.GetAtomType(Path) ?? DMValueType.Anything;

public override void EmitPushValue(ExpressionContext ctx) {
DMCallArgumentsType argumentsType;
Expand Down
4 changes: 3 additions & 1 deletion DMCompiler/DM/Expressions/Constant.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.Diagnostics.CodeAnalysis;
using System.IO;
using DMCompiler.Compiler;
using DMCompiler.Json;
using OpenDreamShared.Common;
using OpenDreamShared.Common.DM;
using OpenDreamShared.Common.Json;

namespace DMCompiler.DM.Expressions;

Expand Down
4 changes: 3 additions & 1 deletion DMCompiler/DM/Expressions/Dereference.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using DMCompiler.Bytecode;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using DMCompiler.Compiler;
using OpenDreamShared.Common;
using OpenDreamShared.Common.DM;
using OpenDreamShared.Common.Bytecode;

namespace DMCompiler.DM.Expressions;

Expand Down
4 changes: 3 additions & 1 deletion DMCompiler/DM/Expressions/LValue.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Diagnostics.CodeAnalysis;
using DMCompiler.Bytecode;
using DMCompiler.Compiler;
using OpenDreamShared.Common;
using OpenDreamShared.Common.Bytecode;
using OpenDreamShared.Common.DM;

namespace DMCompiler.DM.Expressions;

Expand Down
4 changes: 3 additions & 1 deletion DMCompiler/DM/Expressions/Procs.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Linq;
using DMCompiler.Bytecode;
using DMCompiler.Compiler;
using OpenDreamShared.Common;
using OpenDreamShared.Common.Bytecode;
using OpenDreamShared.Common.DM;

namespace DMCompiler.DM.Expressions;

Expand Down
2 changes: 1 addition & 1 deletion DMCompiler/DM/Expressions/Unary.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Diagnostics.CodeAnalysis;
using DMCompiler.Bytecode;
using OpenDreamShared.Common.Bytecode;

namespace DMCompiler.DM.Expressions;

Expand Down
33 changes: 28 additions & 5 deletions DMCompiler/DMCompiler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using DMCompiler.Bytecode;
using DMCompiler.Compiler.DM;
using DMCompiler.Compiler.DMM;
using DMCompiler.Compiler.DMPreprocessor;
Expand All @@ -13,8 +12,12 @@
using DMCompiler.Compiler;
using DMCompiler.Compiler.DM.AST;
using DMCompiler.DM.Builders;
using DMCompiler.Json;
using DMCompiler.Optimizer;
using OpenDreamShared.Common.DM;
using OpenDreamShared.Common;
using OpenDreamShared.Common.Json;
using OpenDreamShared.Common.Bytecode;
using OpenDreamNoClient.Bytecode;

namespace DMCompiler;

Expand Down Expand Up @@ -78,7 +81,7 @@
if (ErrorCount > 0) {
successfulCompile = false;
} else {
var output = SaveJson(maps, preprocessor.IncludedInterface, outputFile);
var output = SaveJson(maps, preprocessor.IncludedInterface, outputFile, new OpcodeVerifier());
if (ErrorCount > 0) {
successfulCompile = false;
} else {
Expand Down Expand Up @@ -294,10 +297,10 @@
return maps;
}

private string SaveJson(List<DreamMapJson> maps, string interfaceFile, string outputFile) {
private string SaveJson(List<DreamMapJson> maps, string interfaceFile, string outputFile, IOpcodeVerifier verifier) {
var jsonRep = DMObjectTree.CreateJsonRepresentation();
var compiledDream = new DreamCompiledJson {
Metadata = new DreamCompiledJsonMetadata { Version = OpcodeVerifier.GetOpcodesHash() },
Metadata = new DreamCompiledJsonMetadata { Version = verifier.GetOpcodesHash() },
Strings = DMObjectTree.StringTable,
Resources = DMObjectTree.Resources.ToArray(),
Maps = maps,
Expand Down Expand Up @@ -389,6 +392,26 @@

return ret;
}

public DMValueType? GetAtomType(DreamPath? path) {
if (path == null) {
return null;
}

if (!DMObjectTree.TryGetDMObject((DreamPath)path, out var dmType))
return DMValueType.Anything;

if (dmType.IsSubtypeOf(DreamPath.Obj))
return DMValueType.Obj;
if (dmType.IsSubtypeOf(DreamPath.Mob))
return DMValueType.Mob;
if (dmType.IsSubtypeOf(DreamPath.Turf))
return DMValueType.Turf;
if (dmType.IsSubtypeOf(DreamPath.Area))
return DMValueType.Area;

return DMValueType.Anything;
}
}

public struct DMCompilerSettings {
Expand Down
5 changes: 5 additions & 0 deletions DMCompiler/DMCompiler.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
<DMStandard Include="DMStandard\**" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\OpenDreamCommon\OpenDreamCommon.csproj" />
<ProjectReference Include="..\OpenDreamNoClient\OpenDreamNoClient.csproj" />
</ItemGroup>

<Target Name="CopyDMStandard" AfterTargets="AfterBuild">
<Copy SourceFiles="@(DMStandard)" DestinationFiles="@(DMStandard->'$(OutDir)\DMStandard\%(RecursiveDir)%(Filename)%(Extension)')" />
</Target>
Expand Down
Loading
Loading