Skip to content

Commit

Permalink
[mycpp/demo] Playing around with schema syntax in C++ files
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy C committed Jan 8, 2023
1 parent f8420f0 commit 48aee9d
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 4 deletions.
75 changes: 71 additions & 4 deletions mycpp/demo/target_lang.cc
Original file line number Diff line number Diff line change
Expand Up @@ -691,10 +691,18 @@ TEST param_passing_demo() {

#define ENUM(name, schema)

#define ENUM2(name, ...)
#define SUM(name, ...)

#define VARIANT(...)

#define USE(path)

#define SUM_NS(name)

#define PROD(name) struct name

#define SCHEMA(name)

TEST tea_macros_demo() {
// The preprocessor does NOT expand this. Instead we have a separate parser
// that does it. Hm not bad.
Expand Down Expand Up @@ -724,12 +732,71 @@ TEST tea_macros_demo() {
//
// OK () and , looks better, but no line breaking. Maybe there is a
// clang-format option.
//
// Enabled WhitespaceSensitiveMacros for now.

SUM(suffix_op,
Nullary #Token,
Unary(Token word, Word arg_word),
Static(Token tok, Str arg)
);

SUM(suffix_op,

Nullary #Token;
Unary {
Token word;
Word arg_word;
}
Static {
Token tok;
Str arg;
}
);

ENUM2(suffix_op,
// The C++ compiler parses and validates these
// Problem: recursive types and so forth. We would need forward declarations
// and all that?
// It's also a bit more verbose.
// How to do the % reference? typedef?

PROD(Token) {
int id;
Str val;
};
struct Word {};

SUM_NS(suffix_op) {
// typedef Token Nullary;
struct Unary {
Token op;
Word arg_word;
};
}

SCHEMA(
data Token(Id id, Str val);

Nullary #Token, Unary(Token word, Word arg_word),
Binary(Token word, Word arg_word), Static(Token tok, Str arg)
enum suffix_op {
Nullary %Token
| Unary(Token op, Word arg_word)
}

// I guess we retain * for reference semantics and so forth
// *out = val; can be useful

data Other(Word[] words, Dict<Str, Word>* mydict, Str? option);

// List<Word>* is also possible, but a bit verbose
// Word words[] would be more like C++
//
// Probably want something more clearly different like:
//
// Word... words
// [Word] words -- synonym for List<Word>* words
// Word@ words -- not bad, for repetition
//
// There are also grammars with + and [] though
);

printf("Sum types defined");
Expand Down
5 changes: 5 additions & 0 deletions test/lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ clang-format() {
AllowShortFunctionsOnASingleLine: None,
AllowShortBlocksOnASingleLine: false,
IndentPPDirectives: BeforeHash,
# Does not do what I want
# TypenameMacros: ["SUM", "VARIANT"]
WhitespaceSensitiveMacros: ["SUM", "VARIANT", "SCHEMA"]
TypenameMacros: ["SUM_NS", "PROD"]
}
'
# We have a lot of switch statements, and the extra indent doesn't help.
Expand Down

0 comments on commit 48aee9d

Please sign in to comment.