Comments (3)
Whoa, what? I haven't heard of that, can you link to one? Why would anyone prefer s-expressions - obviously not me, otherwise I'd probably be coding in LISP right now.
It would be very easy to write a s-expression-to-Loyc-tree parser as a way to make it possible to write C# as s-expressions. You could just re-use the LES lexer, but switch out the parser to something that recognizes s-expressions. In fact, as a "thank you" for posting a message here, I've written a complete and functional S-Expression parser:
using System(, .Collections(, .Generic), .Text, .Linq);
using Loyc(, .Collections, .Syntax (, .Lexing, .Les), .Ecs);
using TT = Loyc.Syntax.Les.TokenType;
// Parser for S-expressions => Loyc trees
// It doesn't support attributes - it's really just an example
public partial class SExprParser : BaseParserForList<Token, int>
{
public static LNode Parse(UString sexpr, string filename = "", IMessageSink msgs = null)
{
var lexer = LesLanguageService.Value.Tokenize(sexpr, filename, msgs);
var withoutComments = new WhitespaceFilter(lexer).Buffered();
var parser = new SExprParser(withoutComments, lexer.SourceFile, msgs);
return parser.Atom();
}
protected LNodeFactory F;
public SExprParser(IList<Token> tokens, ISourceFile file, IMessageSink messageSink, int startIndex = 0)
: base(tokens, default(Token), file, startIndex)
{
ErrorSink = messageSink;
}
protected override void Reset(IList<Token> list, Token eofToken, ISourceFile file, int startIndex = 0)
{
base.Reset(list, eofToken, file, startIndex);
F = new LNodeFactory(file);
}
protected override string ToString(int tokenType)
{
return ((TT)tokenType).ToString();
}
LLLPG (parser(laType(TT), matchType(int), terminalType(Token), allowSwitch(true)));
alias("(" = TT.LParen);
alias(")" = TT.RParen);
alias("." = TT.Dot);
rule LNode Atom() @{
result:List
| t:(TT.Id|"."|TT.Assignment|TT.NormalOp|TT.PreOrSufOp|TT.PrefixOp|TT.Colon|TT.Not|TT.BQString)
{ $result=F.Id((Symbol) t.Value, t.StartIndex, t.EndIndex);}
| t:TT.Literal { $result=F.Literal(t.Value, t.StartIndex, t.EndIndex);}
};
rule LNode List() @{
("("|TT.SpaceLParen) ")"
{return F.List(VList<LNode>.Empty, $"(".StartIndex, $")".EndIndex);}
| {var parts = VList<LNode>.Empty;}
("("|TT.SpaceLParen) target:Atom [parts+=Atom]* ")"
{return F.Call($target, $parts, $"(".StartIndex, $")".EndIndex);}
};
}
Of course, since parsing s-expressions is so easy, this is about 66% boilerplate! Here's some code demonstrating that it works:
LNode @using = SExprParser.Parse("(#import (. System Collections))");
Console.WriteLine(EcsLanguageService.Value.Print(@using));
LNode assign = SExprParser.Parse("(= x (+ x 2))");
Console.WriteLine(EcsLanguageService.Value.Print(assign));
The output is
using System.Collections;
x = x + 2;
The main task, then, would be to write a series of macros to perform the desired transformations.
from ecsharp.
My favourite javascript s-expression transpiler is this one:
https://github.com/anko/eslisp
from ecsharp.
Note: that Javascript s-expr parser reminded me of the obvious, that operators can be treated as if they were identifiers. So I just updated the s-expr parser with a friendlier input syntax for operators.
from ecsharp.
Related Issues (20)
- Base compileTime on dotnet-script CLI tool instead of Roslyn scripting HOT 11
- Support F#ish object expression or Java#ish anonymous clases HOT 2
- Add `define` macro with first argument treated as `this` to enable chaining HOT 29
- Comments before multi-using statements are erased HOT 4
- await fluent operator HOT 1
- Error when transforming the `typeof(X<>)` construct HOT 2
- The error Semicolon': expected Colon for the specific code HOT 2
- Support C# 9 pattern matching HOT 3
- Ref locals cannot have parentheses
- EC#: Syntax error in `Foo<T?>`
- Qualified names of identifier macros not working?
- 'with' or quick binding operator bug HOT 1
- EC#: [return: ...] attribute sometimes causes IndexOutOfRangeException in InternalList
- EC#: `#pragma warning` is not propagated to output file
- EC#: Add support for suffix `!` operator
- EC#: New `?>` operator causes parser errors in code like `X<T?>` HOT 1
- Lemp integration - custom macro is not been called
- Lemp - Match only on attribute name
- Add Deconstruct Method To All LNode types to use c# pattern matching
- FPL Abs bug HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ecsharp.