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

Loyc.Syntax - how are classes (etc) represented? #4

Open
furesoft opened this issue Jul 11, 2018 · 2 comments
Open

Loyc.Syntax - how are classes (etc) represented? #4

furesoft opened this issue Jul 11, 2018 · 2 comments

Comments

@furesoft
Copy link

does a class node for example exists in loyc.syntax? its unusual to make an ast only for commands. i cant map properties events classes and so on?

@qwertie
Copy link
Owner

qwertie commented Jul 11, 2018

Loyc.Syntax implements Loyc trees, which only define literals, identifiers, calls and attributes. EC# is defined in terms of these concepts. For example class Foo {} is represented as a call to #class, specifically #class(Foo, #(), {}). (edit: corrected) Similarly, {} is represented as a call to '{}.

Using the Loyc.Ecs package you can construct one of these call nodes from text using ParseSingle, e.g.

var node = EcsLanguageService.Value.ParseSingle("public class Derived: Base {}");

There are methods in EcsValidators to deconstruct these nodes, but they are sometimes not intuitive to use. In the case of classes you can do

LNode name, baseList, body;
Symbol kind = EcsValidators.SpaceDefinitionKind(node, out name, out bases, out body);
if (kind == CodeSymbols.Class)
    Console.WriteLine("class {0} has these base types: {1}", name.Name, bases);
else
    Console.WriteLine("it wasn't a class");

It's called SpaceDefinitionKind because it detects any kind of "name space" - namespace, class, struct, interface, or enum.

If you're using LeMP/EC# it is easier to construct and detect these syntax trees using the quote and matchCode macros. Here is some EC# code that constructs and then deconstructs the same code:

LNode node = quote {
    public class Derived: Base {}
};
matchCode(node) {
    case { class $name: $(..baseList) { $(.._); } }:
        Console.WriteLine("class {0} has these base types: {1}",
            name.Name, String.Join(" ", baseList));
    default:
        Console.WriteLine("it wasn't a class");
}

Tips:

  • $(..baseList) means to match a list of nodes, not just a single base
  • The _ in $(.._); means "I don't care what's in here so don't create a variable to hold this information".
  • The semicolon in $(.._); is important - the code won't work without it, but currently you don't get an error message when it is missing.

@qwertie qwertie changed the title loy.syntax class loyc.syntax class Jul 11, 2018
@qwertie qwertie changed the title loyc.syntax class Loyc.Syntax - how are classes (etc) represented? Jul 11, 2018
@qwertie
Copy link
Owner

qwertie commented Jul 11, 2018

It should be understood that Loyc trees themselves do not understand classes, functions, events, properties or anything else. Loyc trees are like XML: XML doesn't understand that <p>This</p> is a paragraph, because <p> is defined by HTML, not XML. In the same way, the LES code #fn(#int32, Five, #(), 5) represents the function int Five() => 5; in EC#, but LES doesn't know that. LES and Loyc trees don't have a concept of "functions" in the same way that XML doesn't have a concept of "paragraphs".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants