GithubHelp home page GithubHelp logo

Comments (3)

qwertie avatar qwertie commented on May 30, 2024

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.

ktodyruik avatar ktodyruik commented on May 30, 2024

My favourite javascript s-expression transpiler is this one:

https://github.com/anko/eslisp

from ecsharp.

qwertie avatar qwertie commented on May 30, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.