Skip to content
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
38 changes: 19 additions & 19 deletions GraceLanguage/Execution/AST.cs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,10 @@ private static GraceObject mReportWith(EvaluationContext ctx,
var message = GraceString.AsNativeString(ctx, req[0].Arguments[1]);
var dict = new Dictionary<string, string>();
var pairs = req[1].Arguments[0];
Iterables.ForEach(ctx, pairs,
GraceSequence.ForEach(ctx, pairs,
GraceBlock.Create((GraceObject o) => {
string k = null, v = null;
Iterables.ForEach(ctx, o,
GraceSequence.ForEach(ctx, o,
GraceBlock.Create((GraceObject kv) => {
if (k == null)
k = GraceString.AsNativeString(ctx, kv);
Expand Down Expand Up @@ -865,7 +865,7 @@ protected override void addMethods()

private static GraceObject mParts(RequestNode self)
{
return GraceVariadicList.Of(self.parts);
return GraceSequence.Of(self.parts);
}

/// <inheritdoc/>
Expand Down Expand Up @@ -1174,12 +1174,12 @@ private static GraceObject mName(RequestPartNode self)

private static GraceObject mArguments(RequestPartNode self)
{
return GraceVariadicList.Of(self.Arguments);
return GraceSequence.Of(self.Arguments);
}

private static GraceObject mTypeArguments(RequestPartNode self)
{
return GraceVariadicList.Of(self.GenericArguments);
return GraceSequence.Of(self.GenericArguments);
}

private static GraceObject mAccept(EvaluationContext ctx,
Expand Down Expand Up @@ -1495,7 +1495,7 @@ protected override void addMethods()

private static GraceObject mBody(ObjectConstructorNode self)
{
return GraceVariadicList.Of(self.Body);
return GraceSequence.Of(self.Body);
}

/// <inheritdoc/>
Expand Down Expand Up @@ -1640,7 +1640,7 @@ public virtual GraceObject Respond(EvaluationContext ctx,
if (idNode.Variadic)
{
hadVariadic = true;
var gvl = new GraceVariadicList();
var gvl = new GraceSequence();
for (var i = sigPart.Parameters.Count - 1;
i < pp.req.Arguments.Count;
i++)
Expand Down Expand Up @@ -1672,7 +1672,7 @@ public virtual GraceObject Respond(EvaluationContext ctx,
string name = idNode.Name;
if (idNode.Variadic)
{
var gvl = new GraceVariadicList();
var gvl = new GraceSequence();
myScope.AddLocalDef(name, gvl);
}
}
Expand Down Expand Up @@ -1816,7 +1816,7 @@ private static GraceObject mSignature(MethodNode self)

private static GraceObject mBody(MethodNode self)
{
return GraceVariadicList.Of(self.body);
return GraceSequence.Of(self.body);
}

private static GraceObject mAnnotations(MethodNode self)
Expand Down Expand Up @@ -1928,12 +1928,12 @@ protected override void addMethods()

private static GraceObject mParameters(BlockNode self)
{
return GraceVariadicList.Of(self.Parameters);
return GraceSequence.Of(self.Parameters);
}

private static GraceObject mBody(BlockNode self)
{
return GraceVariadicList.Of(self.Body);
return GraceSequence.Of(self.Body);
}

/// <inheritdoc/>
Expand Down Expand Up @@ -2651,7 +2651,7 @@ protected override void addMethods()

private static GraceObject mSignatures(InterfaceNode self)
{
return GraceVariadicList.Of(self.body);
return GraceSequence.Of(self.body);
}

/// <inheritdoc/>
Expand Down Expand Up @@ -2928,7 +2928,7 @@ private static GraceObject mExcludes(InheritsNode self)
foreach (var e in self.Excludes)
myexcludes.Add(GraceString.Create(e));

return GraceVariadicList.Of(myexcludes);
return GraceSequence.Of(myexcludes);
}

private static GraceObject mAliases(InheritsNode self)
Expand All @@ -2942,10 +2942,10 @@ private static GraceObject mAliases(InheritsNode self)
IList<Node> myannotations = new List<Node>();
foreach (var a in kv.Value.Annotations)
myannotations.Add(a);
oneAlias.Add(GraceVariadicList.Of(myannotations));
myaliases.Add(GraceVariadicList.Of(oneAlias));
oneAlias.Add(GraceSequence.Of(myannotations));
myaliases.Add(GraceSequence.Of(oneAlias));
}
return GraceVariadicList.Of(myaliases);
return GraceSequence.Of(myaliases);
}

/// <inheritdoc/>
Expand Down Expand Up @@ -3181,7 +3181,7 @@ protected override void addMethods()

private static GraceObject mParts(SignatureNode self)
{
return GraceVariadicList.Of(self.Parts);
return GraceSequence.Of(self.Parts);
}

private static GraceObject mReturnType(SignatureNode self)
Expand Down Expand Up @@ -3309,12 +3309,12 @@ private static GraceObject mName(OrdinarySignaturePartNode self)
private static GraceObject mTypeParameters(
OrdinarySignaturePartNode self)
{
return GraceVariadicList.Of(self.GenericParameters);
return GraceSequence.Of(self.GenericParameters);
}

private static GraceObject mParameters(OrdinarySignaturePartNode self)
{
return GraceVariadicList.Of(self.Parameters);
return GraceSequence.Of(self.Parameters);
}

/// <inheritdoc/>
Expand Down
6 changes: 1 addition & 5 deletions GraceLanguage/Execution/ExecutionTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -392,18 +392,14 @@ public Node Visit(BlockParseNode d)
{
parameters.Add(p.Visit(this));
}
else if (p is ParenthesisedParseNode)
else
{
var tok = p.Token;
var it = new IdentifierToken(tok.module, tok.line,
tok.column, "_");
id = new IdentifierParseNode(it);
parameters.Add(new ParameterNode(tok, id, p.Visit(this)));
}
else
{
throw new Exception("unimplemented - unusual parameters");
}
}
var ret = new BlockNode(d.Token, d,
parameters,
Expand Down
8 changes: 5 additions & 3 deletions GraceLanguage/GraceLanguage.csproj
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
Expand Down Expand Up @@ -54,9 +54,9 @@
<Compile Include="Parsing\Parser.cs" />
<Compile Include="Parsing\Tokens.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Runtime\GraceSequence.cs" />
<Compile Include="Runtime\UserObject.cs" />
<Compile Include="Runtime\LocalScope.cs" />
<Compile Include="Runtime\Iterables.cs" />
<Compile Include="Runtime\Exceptions.cs" />
<Compile Include="Runtime\GraceType.cs" />
<Compile Include="Runtime\MethodRequest.cs" />
Expand All @@ -80,6 +80,7 @@
<Compile Include="Utility\Args.cs" />
<Compile Include="Utility\WebSocketEndpoint.cs" />
<Compile Include="Utility\UnicodeLookup.cs" />
<Compile Include="Runtime\Callback.cs" />
</ItemGroup>
<ItemGroup>
<None Include="prelude.grace">
Expand Down Expand Up @@ -115,6 +116,7 @@
</ItemGroup>
<ItemGroup>
<NativeModule Include="modules/platform\memory.cs" />
<NativeModule Include="modules/platform\KernanParser.cs" />
<NativeModule Include="modules/websocket\dom.cs" />
<NativeModule Include="modules/websocket\serve.cs" />
</ItemGroup>
Expand Down Expand Up @@ -146,4 +148,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
4 changes: 3 additions & 1 deletion GraceLanguage/Parsing/Lexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -614,11 +614,13 @@ private Token lexStringRemainder()
{
char c = code[index];
if (c == 'n')
b.Append('\u2028');
b.Append('\n');
else if (c == 't')
b.Append('\t');
else if (c == 'l')
b.Append('\u2028');
else if (c == 'e')
b.Append('\x1b');
else if (c == '{')
b.Append('{');
else if (c == '}')
Expand Down
71 changes: 9 additions & 62 deletions GraceLanguage/Parsing/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -399,20 +399,8 @@ StatementLevel level
"May not have ${token} inside ${context}.");
return null;
}
takeSemicolon();
if (!(lexer.current is NewLineToken
|| lexer.current is CommentToken
|| lexer.current is EndToken
|| lexer.current is RBraceToken))
{
if (start.line == lexer.current.line
|| lexer.current.line == lexer.previous.line)
reportError("P1004", lexer.current,
"Unexpected token after statement.");
else
reportError("P1030", lexer.current,
"Unexpected continuation token after statement.");
}
if (lexer.current is SemicolonToken)
lexer.NextToken(); // use semicolon to end statement
while (lexer.current is NewLineToken)
lexer.NextToken();
attachComments(ret, comments);
Expand Down Expand Up @@ -871,10 +859,6 @@ private List<ParseNode> parseTypeBody()
attachComments(sig, comments);
restoreComments(origComments);
consumeBlankLines();
if (sig.Token.line == lexer.current.line
&& lexer.current.line != start.line)
reportError("P1004", lexer.current,
"Unexpected token after statement.");
lastSig = sig;
}
lexer.NextToken();
Expand Down Expand Up @@ -1220,6 +1204,12 @@ private ParseNode parseExpressionNoBind()
private ParseNode parseExpression()
{
var start = lexer.current;
if (start is NewLineToken)
{
nextToken();
return parseExpression();
}

ParseNode lhs = parseExpressionNoBind();
if (lexer.current is BindToken)
{
Expand Down Expand Up @@ -1300,6 +1290,7 @@ private ParseNode parseTerm()
ret = new IdentifierParseNode((SelfKeywordToken)lexer.current);
nextToken();
}

if (ret == null)
{
reportError("P1018", lexer.current, "Expected term.");
Expand Down Expand Up @@ -1443,23 +1434,6 @@ private ParseNode parseOperatorStream(ParseNode lhs)
bool allArith = true;
while (tok != null)
{
if ((!tok.SpaceBefore || !tok.SpaceAfter))
{
if (tok.Name.StartsWith(":="))
reportError("P1038",
new Dictionary<string, string>
{
{ "rest", tok.Name.Substring(2) }
},
":= needs space before prefix operator"
);
reportError("P1020",
new Dictionary<string, string>()
{
{ "operator", tok.Name }
},
"Infix operators must be surrounded by spaces.");
}
nextToken();
if (lexer.current is CommentToken)
{
Expand Down Expand Up @@ -1559,17 +1533,6 @@ private void parseBraceDelimitedBlock(List<ParseNode> body,
Token lastToken = lexerCurrent();
while (awaiting<RBraceToken>(start))
{
if (lexer.current.column != indentColumn)
{
reportError("P1016", new Dictionary<string, string>()
{
{ "required indentation", "" + (indentColumn - 1) },
{ "given indentation", "" + (lexer.current.column - 1) }
},
"Indentation mismatch; is "
+ (lexer.current.column - 1) + ", should be "
+ (indentColumn - 1) + ".");
}
body.Add(parseStatement(level));
if (lexer.current == lastToken)
{
Expand Down Expand Up @@ -1601,11 +1564,6 @@ private void takeSemicolon()
if (lexer.current is SemicolonToken)
{
lexer.NextToken();
if (!(lexer.current is NewLineToken
|| lexer.current is CommentToken
|| lexer.current is EndToken
|| lexer.current is RBraceToken))
reportError("P1003", "Other code cannot follow a semicolon on the same line.");
}
}

Expand Down Expand Up @@ -1704,17 +1662,6 @@ private ParseNode parseBlock()
indentColumn = firstBodyToken.column;
while (awaiting<RBraceToken>(start))
{
if (lexer.current.column != indentColumn)
{
reportError("P1016", new Dictionary<string, string>
{
{ "required indentation", "" + (indentColumn - 1) },
{ "given indentation", "" + (lexer.current.column - 1) }
},
"Indentation mismatch; is "
+ (lexer.current.column - 1) + ", should be "
+ (indentColumn - 1) + ".");
}
ret.Body.Add(parseStatement(StatementLevel.MethodLevel));
if (lexer.current == lastToken)
{
Expand Down
47 changes: 47 additions & 0 deletions GraceLanguage/Runtime/Callback.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using Grace.Execution;

namespace Grace.Runtime
{
// A new version of "Method.cs" that's much easier to use!
public static class Callback
{
/// <summary> A callback for a method on a receiver with type T0, and taking any number of arguments </summary>
public static Method Nary<T0>(Func<EvaluationContext, T0, MethodRequest, GraceObject> m) where T0 : GraceObject
=> new DelegateMethodInheritable((ctx, req, self) => m(ctx, (T0)self, req));

/// <summary> A callback for a method on a receiver with type T0, and taking no arguments </summar>
public static Method Nullary<T0>(Func<EvaluationContext, T0, GraceObject> m) where T0 : GraceObject
=> CustomArity<T0>((ctx, self, req) => m(ctx, self));

/// <summary> A callback for a method on a receiver with type T0, and taking one argument of type T1 </summar>
public static Method Unary<T0, T1>(Func<EvaluationContext, T0, T1, GraceObject> m) where T0 : GraceObject where T1 : GraceObject
=> CustomArity<T0>((ctx, self, req) => m(ctx, self, GetArg<T1>(ctx, req, 0, 0)), 1);

/// <summary> A callback for a method on a receiver with type T0, and taking two arguments of type T1 and T2 in one part </summar>
public static Method Binary<T0, T1, T2>(Func<EvaluationContext, T0, T1, T2, GraceObject> m) where T0 : GraceObject where T1 : GraceObject where T2 : GraceObject
=> CustomArity<T0>((ctx, self, req) => m(ctx, self, GetArg<T1>(ctx, req, 0, 0), GetArg<T2>(ctx, req, 0, 1)), 2);

/// <summary> A callback for a method on a receiver with type T0, and taking one argument of type T1 in the first part, and one of type T2 in the second part </summar>
public static Method UnaryUnary<T0, T1, T2>(Func<EvaluationContext, T0, T1, T2, GraceObject> m) where T0 : GraceObject where T1 : GraceObject where T2 : GraceObject
=> CustomArity<T0>((ctx, self, req) => m(ctx, self, GetArg<T1>(ctx, req, 0, 0), GetArg<T2>(ctx, req, 1, 0)), 1, 1);

// Utility method to check the arity before calling "m"
private static Method CustomArity<T0>(Func<EvaluationContext, T0, MethodRequest, GraceObject> m, params int[] arities) where T0 : GraceObject
=> Nary<T0>((ctx, self, req) => {
MethodHelper.CheckArity(ctx, req, arities);
return m(ctx, self, req);});

// A utility method to convert types accordingly
private static T GetArg<T>(EvaluationContext ctx, MethodRequest req, int part, int index) where T : GraceObject {
var arg = req[part].Arguments[index].FindNativeParent<T>();
if (arg == null) ErrorReporting.RaiseError(ctx, "R2001",
new Dictionary<string, string> {
{ "method", req.Name },
{ "index", index.ToString() },
{ "part", req[part].Name }
}, "must be a " + typeof(T).Name.Replace("Grace", ""));
return arg; }
}
}
Loading