Skip to content
Permalink
Browse files

more syntax

  • Loading branch information...
stefanbohne committed Jul 17, 2017
1 parent c8c1761 commit fc11e467d00910d1a0924306b6a272609e0dca6c
@@ -10,7 +10,7 @@ import org.bynar.versailles.JanusClass
import org.bynar.versailles.Irreversible
import org.junit.Test
import org.junit.runner.RunWith
import org.bynar.versailles.xtext.versaillesLang.Statements
import org.bynar.versailles.xtext.versaillesLang.BlockStmt
import org.bynar.versailles.Statement
import org.eclipse.xtext.testing.XtextRunner
import org.eclipse.xtext.testing.InjectWith
@@ -195,7 +195,7 @@ class Converter {
}
}

def fromStatements(it: Statements): v.Statement =
def fromBlockStmt(it: BlockStmt): v.Statement =
fromStatements(it.getStatements, it)
def fromStatements(seq: Seq[Statement], it: EObject): v.Statement =
if (seq.isEmpty)
@@ -215,6 +215,8 @@ class Converter {
Seq(v.Sequence().putAnnotation(source, it))
case it: FailStmt =>
Seq(v.Fail().putAnnotation(source, it))
case it: BlockStmt =>
Seq(fromBlockStmt(it))
case it: LetStmt =>
if (it.getValue() != null)
Seq(v.Let(fromExpression(it.getPattern),
@@ -224,6 +226,9 @@ class Converter {
fromExpression(it.getPattern)).
putAnnotation(source, it).
putAnnotation(letInfo, LetAsAssert))
case it: CallStmt =>
Seq(v.Let(v.Tuple().putAnnotation(source, it),
fromExpression(it.getValue)).putAnnotation(source, it))
case it: IfStmt =>
Seq(v.IfStmt(fromExpression(it.getCondition),
fromStatements(Seq(it.getThen), it.getThen),
@@ -366,7 +371,7 @@ class Converter {
v.Lambda(v.Irreversible().putAnnotation(source, it),
v.Variable(v.VariableIdentity.setName(new v.VariableIdentity(), 'it), true).putAnnotation(source, it),
fromExpression(it.getDescription).putAnnotation(source, it)))
val base = v.Def(id, v.Module(fromStatements(it.getStatements)).putAnnotation(source, it)).putAnnotation(source, it)
val base = v.Def(id, v.Module(fromBlockStmt(it.getStatements)).putAnnotation(source, it)).putAnnotation(source, it)
Seq((base /: it.getPath.getSteps.take(it.getPath.getSteps.size - 1)){
case (i, n) =>
v.Def(v.VariableIdentity.setName(new v.VariableIdentity, Symbol(n)),
@@ -6,14 +6,18 @@ generate versaillesLang "http://www.bynar.org/versailles/xtext/VersaillesLang"


CompilationUnit hidden(WS, ML_COMMENT, SL_COMMENT):
{CompilationUnit} statements+=AnnotatedStmt (";"+ statements+=ComplexStatement)* (";"+ "return" expression=Expression)? ";"*
{CompilationUnit} (language=Language ";")?
statements+=AnnotatedStmt
(";"+ statements+=ComplexStatement)*
(";"+ "return" expression=Expression)? ";"*
;
Language returns Language: {VersaillesLanguage} "versailles" "(" major=NUMBER "," minor=NUMBER ")";


Statements: {Statements} "{" statements+=AnnotatedStmt (";"+ statements+=ComplexStatement)* ";"* "}";
BlockStmt: {BlockStmt} "{" ";"* statements+=AnnotatedStmt (";"+ statements+=ComplexStatement)* ";"* "}";
ComplexStatement returns Statement:
AnnotatedStmt
| Statements
| BlockStmt
;
AnnotatedStmt returns Statement:
"@" {AnnotatedStmt} annotation=Expression statement=SimpleStatement
@@ -23,16 +27,18 @@ SimpleStatement returns Statement:
{PassStmt} "pass"
| {FailStmt} "fail"
| {LetStmt} "let" pattern=Expression ("=" value=Expression)?
| {CallStmt} "call" value=Expression
| {ForgetStmt} "forget" pattern=Expression "=" value=Expression
| {RememberStmt} "remember" pattern=Expression "=" value=Expression
| {ForStmt} "for" pattern=Expression "from" container=Expression
| {WhereStmt} ("where" | inverted?="unless") condition=Expression
| {SwitchStmt} "switch" "{" ("case" cases+=ComplexStatement ";"*)* "}"
| {IfStmt} "if" condition=Expression
("then" then=ComplexStatement | then=Statements)
("then" then=ComplexStatement | then=BlockStmt)
("asserting" assertion=Expression)?
("else" else=ComplexStatement)?
| {LoopStmt} ("from" assertion=Expression)? ("do" do=ComplexStatement)? ("loop" loop=ComplexStatement)? ("until" | while?="while") condition=Expression
| {LoopStmt} ("from" assertion=Expression)? ( "do" do=ComplexStatement ("until" condition=Expression)? ("loop" loop=ComplexStatement)?
| ("until" condition=Expression)? "loop" loop=ComplexStatement)
| {DefStmt} ("def"|let?="letdef") name=Name
( (title=STRING)? (description=InterpolatedString)? ":" type=TypeExpression ("=" value=Expression)?
| (arguments+=(TupleExpr|TupleTypeExpr))+ (title=STRING)? (description=InterpolatedString)? (
@@ -42,7 +48,7 @@ SimpleStatement returns Statement:
| (name2=Name)? results=TupleExpr (title2=STRING)? (description2=InterpolatedString)? statements=ComplexStatement)))
| {TypeStmt} ("type"|let?="lettype") name=TypeName (arguments+=(TupleExpr|TupleTypeExpr))* (title=STRING)? (description=InterpolatedString)?
("::" kind=TypeExpression)? ("=" type=TypeExpression)?
| {ModuleStmt} "module" path=NamePath (title=STRING)? (description=InterpolatedString)? statements=Statements
| {ModuleStmt} "module" path=NamePath (title=STRING)? (description=InterpolatedString)? statements=BlockStmt
| {VariantStmt} "variant" name=Name
( (title=STRING)? (description=InterpolatedString)? ":" type=TypeExpression ("=" value=Expression)?
| argument=(TupleExpr|TupleTypeExpr) (title=STRING)? (description=InterpolatedString)? )
@@ -119,7 +125,7 @@ SimpleExpr returns Expression:
| InterpolatedString
| TupleExpr
| {ListExpr} index=IndexExpr
| {BlockExpr} "{" statements+=AnnotatedStmt (";"+ statements+=ComplexStatement)* (";"+ ("return" | yield?="yield") scope=Expression)? ";"* "}"
| {BlockExpr} "{" ";"* statements+=AnnotatedStmt (";"+ statements+=ComplexStatement)* (";"+ ("return" | yield?="yield") scope=Expression)? ";"* "}"
| CasesExpr
| {Type2Expr} type=TupleTypeExpr
;
@@ -130,7 +136,7 @@ InterpolatedString:
;

TupleExpr returns Expression:
{TupleExpr} "(" (components+=TupleComponent ("," components+=TupleComponent)*)? (forceTuple?=",")? ")"
{TupleExpr} "(" (","* components+=TupleComponent (","+ components+=TupleComponent)*)? (forceTuple?="," ","*)? ")"
;
TupleComponent:
(name=Name "=")? value=Expression
@@ -186,9 +192,9 @@ ModifiedType returns TypeExpression:
;
CompoundType returns TypeExpression:
ApplicationType
| {AlgebraicType} "algebraic" statements=Statements
| {InductiveType} "inductive" statements=Statements
| {BigTupleType} "tuple" statements=Statements
| {AlgebraicType} "algebraic" statements=BlockStmt
| {InductiveType} "inductive" statements=BlockStmt
| {BigTupleType} "tuple" statements=BlockStmt
;
ApplicationType returns TypeExpression:
SimpleType ( {ValueApplicationType.function=current} => argument=TupleExpr
@@ -208,10 +214,11 @@ TupleTypeComponent: (name=Name ":")? type=TypeExpression;

TypeName: ID_UPPER | ID_BACKTICK;
Name: ID_LOWER | TypeName |
"algebraic" | "and" | "asserting" | "def" |"do" | "else" |
"fail" | "for" | "forget" | "from" | "if" | "in" | "inductive" |
"algebraic" | "and" | "asserting" | "def" | "div" | "do" | "else" |
"fail" | "for" | "forget" | "from" | "if" | "iff" | "implies" | "in" | "inductive" |
"let" | "letdef" | "lettype" | "loop" | "mod" | "module" | "or" |
"remember" | "return" | "pass" | "then" | "tuple" | "unless" | "until" | "where"| "yield";
"remember" | "return" | "pass" | "switch" | "then" | "tuple" |
"unless" | "until" | "versailles" | "where"| "yield";
NamePath: steps+=Name ("." steps+=Name)*;

terminal ID_LOWER:
@@ -6,6 +6,8 @@ generate bynarLang "http://www.bynar.org/xtext/BynarLang"

@Override
CompilationUnit returns versailles::CompilationUnit: super;
@Override
Language returns Language: {BynarLanguage} "bynar" "(" major=NUMBER "," minor=NUMBER ")";

@Override
SimpleStatement returns versailles::Statement:
@@ -30,14 +32,14 @@ ModifiedType returns versailles::TypeExpression:
CompoundType returns versailles::TypeExpression:
super
| {BitFieldType} "bits" bitWidth=Expression
| {RecordType} "record" statements=Statements
| {UnionType} "union" statements=Statements
| {RegisterType} "register" bitWidth=Expression statements=Statements
| {RecordType} "record" statements=BlockStmt
| {UnionType} "union" statements=BlockStmt
| {RegisterType} "register" bitWidth=Expression statements=BlockStmt
| {ArrayType} "array" "of" elementType=TypeExpression ("length" length=Expression | "until" until=Expression)
;

Interpretation:
{EnumInterpretation} "one" "of" statements=Statements
{EnumInterpretation} "one" "of" statements=BlockStmt
| {FixedInterpretation} "fixed" value=Expression
| {UnitInterpretation} "in" unit=STRING
| {ContainingInterpretation} "containing" containedType=CompoundType
@@ -58,8 +60,8 @@ BitUnit:
@Override
Name:
super |
"align" | "at" | "array" | "bit" |"bits" | "by" | "byte" | "bytes" |
"component" | "containing" | "converted" |
"align" | "at" | "array" | "bit" |"bits" | "by" | "bynar" | "byte" | "bytes" |
"component" | "containing" | "converted" |
"fixed" | "kB" | "gigabyte" | "gigabytes" | "gibibyte" | "gibibytes" |
"kilobytes" | "kilobyte" | "kibibyte" | "kibibytes" | "is" | "length" |
"megabytes" | "megabyte" | "mibibyte" | "mibibytes" | "offset" | "one" | "of" |
@@ -37,7 +37,7 @@ class Converter extends org.bynar.versailles.xtext.Converter {
def fromInterpretation(it: Interpretation): b.BitTypeInterpretation =
it match {
case it: EnumInterpretation =>
b.EnumInterpretation(fromStatements(it.getStatements)).putAnnotation(source, it)
b.EnumInterpretation(fromBlockStmt(it.getStatements)).putAnnotation(source, it)
case it: FixedInterpretation =>
b.FixedInterpretation(fromExpression(it.getValue)).putAnnotation(source, it)
case it: UnitInterpretation =>
@@ -117,12 +117,12 @@ class MemberConverter(val path: Seq[Symbol]) extends Converter {
case it: BitFieldType =>
b.BitFieldType(fromExpression(it.getBitWidth)).putAnnotation(source, it)
case it: RecordType =>
b.BitRecordType(fromStatements(it.getStatements)).putAnnotation(source, it)
b.BitRecordType(fromBlockStmt(it.getStatements)).putAnnotation(source, it)
case it: RegisterType =>
b.BitRegisterType(fromExpression(it.getBitWidth),
fromStatements(it.getStatements)).putAnnotation(source, it)
fromBlockStmt(it.getStatements)).putAnnotation(source, it)
case it: UnionType =>
b.BitUnionType(fromStatements(it.getStatements)).putAnnotation(source, it)
b.BitUnionType(fromBlockStmt(it.getStatements)).putAnnotation(source, it)
case it: ArrayType =>
val u = if (it.getUntil != null)
fromExpression(it.getUntil)

0 comments on commit fc11e46

Please sign in to comment.
You can’t perform that action at this time.