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

Implement sign() #2207

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions Content.Tests/DMProject/Tests/Builtins/sign.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

/proc/RunTest()
ASSERT(sign(5.2) == 1)
ASSERT(sign(-5.2) == -1)
ASSERT(sign(0) == 0)
ASSERT(sign(null) == 0)
ASSERT(sign("") == 0)
ASSERT(sign("foo") == 0)
ASSERT(sign(list(1)) == 0)

1 change: 1 addition & 0 deletions DMCompiler/DMStandard/_Standard.dm
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ proc/rgb(R, G, B, A, space) as text|null
proc/rgb2num(color, space = COLORSPACE_RGB) as /list
proc/roll(ndice = 1, sides) as num
proc/round(A, B) as num
proc/sign(A) as num
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comes after shutdown() alphabetically. Move it in the other two files too.

proc/sha1(input) as text|null
proc/shutdown(Addr,Natural = 0)
proc/sleep(Delay)
Expand Down
1 change: 1 addition & 0 deletions OpenDreamRuntime/Procs/Native/DreamProcNative.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public static void SetupNativeProcs(DreamObjectTree objectTree) {
objectTree.SetGlobalNativeProc(DreamProcNativeRoot.NativeProc_rgb2num);
objectTree.SetGlobalNativeProc(DreamProcNativeRoot.NativeProc_roll);
objectTree.SetGlobalNativeProc(DreamProcNativeRoot.NativeProc_round);
objectTree.SetGlobalNativeProc(DreamProcNativeRoot.NativeProc_sign);
objectTree.SetGlobalNativeProc(DreamProcNativeRoot.NativeProc_sha1);
objectTree.SetGlobalNativeProc(DreamProcNativeRoot.NativeProc_shutdown);
objectTree.SetGlobalNativeProc(DreamProcNativeRoot.NativeProc_sleep);
Expand Down
16 changes: 16 additions & 0 deletions OpenDreamRuntime/Procs/Native/DreamProcNativeRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2380,6 +2380,22 @@ public static DreamValue NativeProc_roll(NativeProc.Bundle bundle, DreamObject?
return new DreamValue(total);
}

[DreamProc("sign")]
[DreamProcParameter("A", Type = DreamValueTypeFlag.Float)]
public static DreamValue NativeProc_sign(NativeProc.Bundle bundle, DreamObject? src, DreamObject? usr) {
if (bundle.Arguments.Length != 1) throw new Exception($"expected 1 argument (found {bundle.Arguments.Length})");
DreamValue arg = bundle.GetArgument(0, "A");

// Any non-num returns 0
if (!arg.TryGetValueAsFloat(out var value)) return new DreamValue(0);

return value switch {
0 => new DreamValue(0),
< 0 => new DreamValue(-1),
_ => new DreamValue(1)
};
}

[DreamProc("sha1")]
[DreamProcParameter("T", Type = DreamValueTypeFlag.String | DreamValueTypeFlag.DreamResource)]
public static DreamValue NativeProc_sha1(NativeProc.Bundle bundle, DreamObject? src, DreamObject? usr) {
Expand Down
Loading