Skip to content

Commit

Permalink
Merge branch 'devel'
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianRappl committed Jul 8, 2018
2 parents 26d0fb8 + e143b2c commit 333e915
Show file tree
Hide file tree
Showing 29 changed files with 3,018 additions and 575 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 1.6.0

- Included `Clip` function (#89)
- Fixed long integer parsing (#88)
- Allow Forced New Lines in REPL (#87)

# 1.5.0

- Added support for named arguments (#65)
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2016 Florian Rappl
Copyright (c) 2016-2018 Florian Rappl

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ MAGES is the official successor to YAMP. It is a very simple, yet powerful, expr

### Current Status

The first stable version has been released. The current version 1.1.0 contains an improved REPL. The library contains everything to perform lightweight scripting operations in C#. A [CodeProject article](http://www.codeproject.com/Articles/1108939/MAGES-Ultimate-Scripting-for-NET) about the library (also containing some background and performance comparisons) is also available.
The first stable version has been released. The current version 1.6.0 contains an improved REPL. The library contains everything to perform lightweight scripting operations in C#. A [CodeProject article](http://www.codeproject.com/Articles/1108939/MAGES-Ultimate-Scripting-for-NET) about the library (also containing some background and performance comparisons) is also available.

### Installation

Expand Down Expand Up @@ -84,7 +84,7 @@ Hence: Do not expect any breaking changes within the same major version.

The MIT License (MIT)

Copyright (c) 2016-2017 Florian Rappl
Copyright (c) 2016-2018 Florian Rappl

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
2 changes: 1 addition & 1 deletion build.cake
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var releaseDir = squirrelRoot + Directory("release");
// Initialization
// ----------------------------------------

Setup(() =>
Setup(context =>
{
Information("Building version {0} of MAGES.", version);
Information("For the publish target the following environment variables need to be set:");
Expand Down
21 changes: 21 additions & 0 deletions src/Mages.Core.Tests/FunctionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,27 @@ public void ClampWithMatrixAllCasesHit()
CollectionAssert.AreEquivalent(new[,] { { 1.0, 2.0 }, { -5.0, -5.0 }, { 5.0, 5.0 } }, (Double[,])result);
}

[Test]
public void ClipWithStringMinCaseHit()
{
var result = "clip(1, 4, \"\") ".Eval();
Assert.AreEqual(" ", result);
}

[Test]
public void ClipWithStringMaxCaseHit()
{
var result = "clip(1, 4, \"foo\") ".Eval();
Assert.AreEqual("oo", result);
}

[Test]
public void ClipWithStringNormalCaseHit()
{
var result = "clip(1, 4, \"foooooo\") ".Eval();
Assert.AreEqual("oooo", result);
}

[Test]
public void LerpWithInterpolationInBounds()
{
Expand Down
12 changes: 12 additions & 0 deletions src/Mages.Core.Tests/NumberTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ public void NumberScannerScientificMinus()
Assert.AreEqual(0.1, ((NumberToken)result).Value);
}

[Test]
public void NumberScannerLargeValue()
{
var source = "9223372036854775807";
var scanner = new StringScanner(source);
Assert.IsTrue(scanner.MoveNext());
var tokenizer = new NumberTokenizer();
var result = tokenizer.Next(scanner);
Assert.IsInstanceOf<NumberToken>(result);
Assert.AreEqual(9223372036854776000.0, ((NumberToken)result).Value);
}

[Test]
public void NumberScannerScientificPlus()
{
Expand Down
113 changes: 78 additions & 35 deletions src/Mages.Core/EngineExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,48 +193,24 @@ public static Future InterpretAsync(this Engine engine, String source)
/// <returns>The plugin if any.</returns>
public static Plugin AddPlugin(this Engine engine, Type type)
{
if (type.IsSealed && type.IsAbstract && type.Name.Length > 6 && type.Name.EndsWith("Plugin"))
if (type.Name.Length > 6 && type.Name.EndsWith("Plugin"))
{
var flags = BindingFlags.Public | BindingFlags.Static;
var fields = type.GetFields(flags);
var properties = type.GetProperties(flags);
var methods = type.GetMethods(flags);
var meta = new Dictionary<String, String>();
var content = new Dictionary<String, Object>();
var plugin = new Plugin(meta, content);

foreach (var field in fields)
if (type.IsSealed && type.IsAbstract)
{
if (field.FieldType == typeof(String))
{
var name = meta.Keys.FindName(field);
var value = field.GetValue(null);
meta.Add(name, value as String);
}
var plugin = ConstructStaticPlugin(type);
engine.AddPlugin(plugin);
return plugin;
}

foreach (var property in properties)
{
if (property.GetIndexParameters().Length == 0 && property.CanRead && !property.IsSpecialName)
{
var name = content.Keys.FindName(property);
var value = property.GetValue(null, null).WrapObject();
content.Add(name, value);
}
}
var constructor = type.GetConstructor(new[] { typeof(Engine) });

foreach (var method in methods)
if (constructor != null)
{
if (!method.IsSpecialName)
{
var name = content.Keys.FindName(method);
var value = method.WrapFunction(null);
content.Add(name, value);
}
var obj = constructor.Invoke(new[] { engine });
var plugin = ConstructInstancePlugin(obj);
engine.AddPlugin(plugin);
return plugin;
}

engine.AddPlugin(plugin);
return plugin;
}

return null;
Expand Down Expand Up @@ -340,6 +316,73 @@ internal static void Apply(this Engine engine, Configuration configuration)
}
}

private static Dictionary<String, String> GetMetadata(Type type)
{
var flags = BindingFlags.Public | BindingFlags.Static;
var fields = type.GetFields(flags);
var meta = new Dictionary<String, String>();

foreach (var field in fields)
{
if (field.FieldType == typeof(String))
{
var name = meta.Keys.FindName(field);
var value = field.GetValue(null);
meta.Add(name, value as String);
}
}

return meta;
}

private static Dictionary<String, Object> GetContent(IEnumerable<PropertyInfo> properties, IEnumerable<MethodInfo> methods, Object instance)
{
var content = new Dictionary<String, Object>();

foreach (var property in properties)
{
if (property.GetIndexParameters().Length == 0 && property.CanRead && !property.IsSpecialName)
{
var name = content.Keys.FindName(property);
var value = property.GetValue(instance, null).WrapObject();
content.Add(name, value);
}
}

foreach (var method in methods)
{
if (!method.IsSpecialName)
{
var name = content.Keys.FindName(method);
var value = method.WrapFunction(instance);
content.Add(name, value);
}
}

return content;
}

private static Plugin ConstructStaticPlugin(Type type)
{
var flags = BindingFlags.Public | BindingFlags.Static;
var properties = type.GetProperties(flags);
var methods = type.GetMethods(flags);
var meta = GetMetadata(type);
var content = GetContent(properties, methods, null);
return new Plugin(meta, content);
}

private static Plugin ConstructInstancePlugin(Object obj)
{
var type = obj.GetType();
var flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly;
var properties = type.GetProperties(flags);
var methods = type.GetMethods(flags);
var meta = GetMetadata(type);
var content = GetContent(properties, methods, obj);
return new Plugin(meta, content);
}

sealed class Placement : IPlacement
{
private readonly Engine _engine;
Expand Down
Loading

0 comments on commit 333e915

Please sign in to comment.