diff --git a/compiler/lib/src/main/scala/analysis/Analyzers/TypeExpressionAnalyzer.scala b/compiler/lib/src/main/scala/analysis/Analyzers/TypeExpressionAnalyzer.scala index 40766c794..3346ba938 100644 --- a/compiler/lib/src/main/scala/analysis/Analyzers/TypeExpressionAnalyzer.scala +++ b/compiler/lib/src/main/scala/analysis/Analyzers/TypeExpressionAnalyzer.scala @@ -48,6 +48,13 @@ trait TypeExpressionAnalyzer def typeNameNode(a: Analysis, node: AstNode[Ast.TypeName]): Result = matchTypeNameNode(a, node) + override def typeNameRangeNode(a: Analysis, node: AstNode[Ast.TypeName], tn: Ast.TypeNameRange) = + for { + a <- typeNameNode(a, tn.typeName) + a <- opt(exprNode)(a, tn.lowerBound._2) + a <- opt(exprNode)(a, tn.upperBound._2) + } yield a + override def typeNameStringNode(a: Analysis, node: AstNode[Ast.TypeName], tn: Ast.TypeNameString) = opt(exprNode)(a, tn.size) diff --git a/compiler/lib/src/main/scala/analysis/CheckSemantics/CheckTypeUses.scala b/compiler/lib/src/main/scala/analysis/CheckSemantics/CheckTypeUses.scala index ff5b731dd..36a8d5290 100644 --- a/compiler/lib/src/main/scala/analysis/CheckSemantics/CheckTypeUses.scala +++ b/compiler/lib/src/main/scala/analysis/CheckSemantics/CheckTypeUses.scala @@ -138,6 +138,13 @@ object CheckTypeUses extends UseAnalyzer { Right(a.assignType(node -> t)) } + override def typeNameRangeNode(a: Analysis, node: AstNode[Ast.TypeName], tn: Ast.TypeNameRange) = + for (a <- super.typeNameRangeNode(a, node, tn)) + yield + // TODO: For now, just assign the base type to the range + val t = a.typeMap(tn.typeName.id) + a.assignType(node -> t) + override def typeNameStringNode(a: Analysis, node: AstNode[Ast.TypeName], tn: Ast.TypeNameString) = for (a <- super.typeNameStringNode(a, node, tn)) yield a.assignType(node -> Type.String(tn.size)) diff --git a/compiler/lib/src/main/scala/ast/Ast.scala b/compiler/lib/src/main/scala/ast/Ast.scala index df0a9be3f..2e51eb50f 100644 --- a/compiler/lib/src/main/scala/ast/Ast.scala +++ b/compiler/lib/src/main/scala/ast/Ast.scala @@ -1001,12 +1001,23 @@ object Ast { } + case object TypeNameBool extends TypeName final case class TypeNameFloat(name: TypeFloat) extends TypeName final case class TypeNameInt(name: TypeInt) extends TypeName final case class TypeNameQualIdent(name: AstNode[QualIdent]) extends TypeName { override def getIsAbsoluteOpt = Some(name.data.isAbsolute) } - case object TypeNameBool extends TypeName + final case class TypeNameRange( + typeName: AstNode[TypeName], + lowerBound: TypeNameRange.Bound, + upperBound: TypeNameRange.Bound + ) extends TypeName + object TypeNameRange { + enum BoundKind: + case INCLUSIVE, EXCLUSIVE + /** A range bound. None represents the bound -- */ + type Bound = (BoundKind, Option[AstNode[Expr]]) + } final case class TypeNameString(size: Option[AstNode[Expr]]) extends TypeName /** Unary operation */ diff --git a/compiler/lib/src/main/scala/ast/AstTransformer.scala b/compiler/lib/src/main/scala/ast/AstTransformer.scala index c3f6c6649..d5dce351e 100644 --- a/compiler/lib/src/main/scala/ast/AstTransformer.scala +++ b/compiler/lib/src/main/scala/ast/AstTransformer.scala @@ -367,6 +367,9 @@ trait AstTransformer { def typeNameQualIdentNode(in: In, node: AstNode[Ast.TypeName], tn: Ast.TypeNameQualIdent): ResultNode[Ast.TypeName] = defaultNode(in, node) + def typeNameRangeNode(in: In, node: AstNode[Ast.TypeName], tn: Ast.TypeNameRange): ResultNode[Ast.TypeName] = + defaultNode(in, node) + def typeNameStringNode(in: In, node: AstNode[Ast.TypeName], tn: Ast.TypeNameString): ResultNode[Ast.TypeName] = defaultNode(in, node) @@ -636,6 +639,7 @@ trait AstTransformer { case tn : Ast.TypeNameFloat => typeNameFloatNode(in, node, tn) case tn : Ast.TypeNameInt => typeNameIntNode(in, node, tn) case tn : Ast.TypeNameQualIdent => typeNameQualIdentNode(in, node, tn) + case tn : Ast.TypeNameRange => typeNameRangeNode(in, node, tn) case tn : Ast.TypeNameString => typeNameStringNode(in, node, tn) } diff --git a/compiler/lib/src/main/scala/ast/AstVisitor.scala b/compiler/lib/src/main/scala/ast/AstVisitor.scala index 6eaf54361..6bd9ce0ef 100644 --- a/compiler/lib/src/main/scala/ast/AstVisitor.scala +++ b/compiler/lib/src/main/scala/ast/AstVisitor.scala @@ -163,6 +163,8 @@ trait AstVisitor { def typeNameQualIdentNode(in: In, node: AstNode[Ast.TypeName], tn: Ast.TypeNameQualIdent): Out = default(in) + def typeNameRangeNode(in: In, node: AstNode[Ast.TypeName], tn: Ast.TypeNameRange): Out = default(in) + def typeNameStringNode(in: In, node: AstNode[Ast.TypeName], tn: Ast.TypeNameString): Out = default(in) final def matchComponentMember(in: In, member: Ast.ComponentMember): Out = { @@ -299,6 +301,7 @@ trait AstVisitor { case tn : Ast.TypeNameFloat => typeNameFloatNode(in, node, tn) case tn : Ast.TypeNameInt => typeNameIntNode(in, node, tn) case tn : Ast.TypeNameQualIdent => typeNameQualIdentNode(in, node, tn) + case tn : Ast.TypeNameRange => typeNameRangeNode(in, node, tn) case tn : Ast.TypeNameString => typeNameStringNode(in, node, tn) } diff --git a/compiler/lib/src/main/scala/codegen/AstWriter.scala b/compiler/lib/src/main/scala/codegen/AstWriter.scala index a03e01c92..5cf896fb0 100644 --- a/compiler/lib/src/main/scala/codegen/AstWriter.scala +++ b/compiler/lib/src/main/scala/codegen/AstWriter.scala @@ -798,6 +798,27 @@ object AstWriter extends AstVisitor with LineUtils { tn: Ast.TypeNameQualIdent ) = addPrefix("type name", qualIdent) (tn.name.data) + override def typeNameRangeNode( + in: In, + node: AstNode[Ast.TypeName], + tn: Ast.TypeNameRange + ) = { + def bound(position: String, b: Ast.TypeNameRange.Bound) = { + val (kind, eOpt) = b + val prefix = s"${boundKind(kind)} $position bound" + eOpt match { + case Some(e) => addPrefix(prefix, exprNode) (e) + case None => lines(s"$prefix --") + } + } + lines("type name range") ++ + List.concat( + typeNameNode(tn.typeName), + bound("lower", tn.lowerBound), + bound("upper", tn.upperBound) + ).map(indentIn) + } + override def typeNameStringNode( in: In, node: AstNode[Ast.TypeName], @@ -846,6 +867,12 @@ object AstWriter extends AstVisitor with LineUtils { private def binop(op: Ast.Binop) = lines(s"binop ${op.toString}") + private def boundKind(kind: Ast.TypeNameRange.BoundKind) = + kind match { + case Ast.TypeNameRange.BoundKind.INCLUSIVE => "inclusive" + case Ast.TypeNameRange.BoundKind.EXCLUSIVE => "exclusive" + } + private def componentMember(member: Ast.ComponentMember) = { val (a1, _, a2) = member.node val l = matchComponentMember((), member) diff --git a/compiler/lib/src/main/scala/codegen/FppWriter.scala b/compiler/lib/src/main/scala/codegen/FppWriter.scala index 502b50c39..b9896e4d7 100644 --- a/compiler/lib/src/main/scala/codegen/FppWriter.scala +++ b/compiler/lib/src/main/scala/codegen/FppWriter.scala @@ -832,6 +832,34 @@ class FppWriter(a: Analysis = Analysis()) extends AstVisitor with LineUtils { tn: Ast.TypeNameQualIdent ) = qualIdent(tn.name.data) + override def typeNameRangeNode( + in: In, + node: AstNode[Ast.TypeName], + tn: Ast.TypeNameRange + ) = { + import Ast.TypeNameRange.BoundKind.{EXCLUSIVE, INCLUSIVE} + def bound(eOpt: Option[AstNode[Ast.Expr]]) = eOpt match { + case Some(e) => exprNode(e) + case None => lines("--") + } + val (lowerKind, lowerOpt) = tn.lowerBound + val (upperKind, upperOpt) = tn.upperBound + val lowerBracket = lowerKind match { + case INCLUSIVE => "[" + case EXCLUSIVE => "(" + } + val upperBracket = upperKind match { + case INCLUSIVE => "]" + case EXCLUSIVE => ")" + } + val range = Line.addPrefixAndSuffix( + lowerBracket, + bound(lowerOpt).join (", ") (bound(upperOpt)), + upperBracket + ) + typeNameNode(tn.typeName).join (" in ") (range) + } + override def typeNameStringNode( in: In, node: AstNode[Ast.TypeName], diff --git a/compiler/lib/src/main/scala/syntax/Lexer.scala b/compiler/lib/src/main/scala/syntax/Lexer.scala index e4a6b1ba6..a36a029b5 100644 --- a/compiler/lib/src/main/scala/syntax/Lexer.scala +++ b/compiler/lib/src/main/scala/syntax/Lexer.scala @@ -74,6 +74,7 @@ object Lexer { ("if", IF), ("implements", IMPLEMENTS), ("import", IMPORT), + ("in", IN), ("include", INCLUDE), ("initial", INITIAL), ("input", INPUT), @@ -282,6 +283,7 @@ object Lexer { case IF => Token.IF() case IMPLEMENTS => Token.IMPLEMENTS() case IMPORT => Token.IMPORT() + case IN => Token.IN() case INCLUDE => Token.INCLUDE() case INITIAL => Token.INITIAL() case INPUT => Token.INPUT() @@ -301,6 +303,7 @@ object Lexer { case MACHINE => Token.MACHINE() case MATCH => Token.MATCH() case MINUS => Token.MINUS() + case MINUS_MINUS => Token.MINUS_MINUS() case MODULE => Token.MODULE() case OMIT => Token.OMIT() case ON => Token.ON() @@ -545,11 +548,16 @@ object Lexer { eatNewlines() case '-' => nextChar() - if (ch == '>') { - token = RARROW + if (ch == '-') { + token = MINUS_MINUS nextChar() - } else token = MINUS - eatNewlines() + } else { + if (ch == '>') { + token = RARROW + nextChar() + } else token = MINUS + eatNewlines() + } case '+' => nextChar() token = PLUS diff --git a/compiler/lib/src/main/scala/syntax/Parser.scala b/compiler/lib/src/main/scala/syntax/Parser.scala index 3d721b582..709905474 100644 --- a/compiler/lib/src/main/scala/syntax/Parser.scala +++ b/compiler/lib/src/main/scala/syntax/Parser.scala @@ -1039,12 +1039,50 @@ object Parser extends Parsers { accept("U32", { case Token.U32() => Ast.TypeNameInt(Ast.U32) }) | accept("U64", { case Token.U64() => Ast.TypeNameInt(Ast.U64) }) - accept("bool", { case Token.BOOL() => Ast.TypeNameBool }) | - string ~> opt(size ~>! exprNode) ^^ (e => Ast.TypeNameString(e)) | - typeNameFloat | - typeNameInt | - node(qualIdent) ^^ (qid => Ast.TypeNameQualIdent(qid)) | - failure("type name expected") + def baseTypeName = + accept("bool", { case Token.BOOL() => Ast.TypeNameBool }) | + string ~> opt(size ~>! exprNode) ^^ (e => Ast.TypeNameString(e)) | + typeNameFloat | + typeNameInt | + node(qualIdent) ^^ (qid => Ast.TypeNameQualIdent(qid)) | + failure("type name expected") + + def rangeBound = + minusMinus ^^ (_ => None) | + exprNode ^^ (e => Some(e)) + + def lowerBoundKind = + lbracket ^^ (_ => Ast.TypeNameRange.BoundKind.INCLUSIVE) | + lparen ^^ (_ => Ast.TypeNameRange.BoundKind.EXCLUSIVE) | + failure("[ or ( expected") + + def upperBoundKind = + rbracket ^^ (_ => Ast.TypeNameRange.BoundKind.INCLUSIVE) | + rparen ^^ (_ => Ast.TypeNameRange.BoundKind.EXCLUSIVE) | + failure("] or ) expected") + + def range = + lowerBoundKind ~! rangeBound ~! (comma ~>! rangeBound) ~! upperBoundKind ^^ { + case lowerKind ~ lower ~ upper ~ upperKind => + ((lowerKind, lower), (upperKind, upper)) + } + + final case class PositionedTypeName(tn: Ast.TypeName) extends Positional + + // A type name followed by zero or more ranges + // Each range wraps the type name to its left in a node + // located at the start of the base type name + positioned(baseTypeName ^^ (tn => PositionedTypeName(tn))) ~ + rep(inToken ~>! range) ^^ { + case ptn ~ ranges => + val loc = Location(ParserState.file, ptn.pos, ParserState.includingLoc) + ranges.foldLeft (ptn.tn) { + case (tn, (lower, upper)) => + val node = AstNode.create(tn) + Locations.put(node.id, loc) + Ast.TypeNameRange(node, lower, upper) + } + } } private def templateParam: Parser[Ast.TemplateParam] = { @@ -1276,6 +1314,8 @@ object Parser extends Parsers { private def importToken = accept("import", { case t: Token.IMPORT => t }) + private def inToken = accept("in", { case t: Token.IN => t }) + private def include = accept("include", { case t: Token.INCLUDE => t }) private def initial = accept("initial", { case t: Token.INITIAL => t }) @@ -1313,6 +1353,8 @@ object Parser extends Parsers { private def minus = accept("-", { case t: Token.MINUS => t }) + private def minusMinus = accept("--", { case t: Token.MINUS_MINUS => t }) + private def module = accept("module", { case t: Token.MODULE => t }) private def omit = accept("omit", { case t: Token.OMIT => t }) diff --git a/compiler/lib/src/main/scala/syntax/Token.scala b/compiler/lib/src/main/scala/syntax/Token.scala index e255b9e0e..90baa8aa3 100644 --- a/compiler/lib/src/main/scala/syntax/Token.scala +++ b/compiler/lib/src/main/scala/syntax/Token.scala @@ -67,6 +67,7 @@ object Token { final case class IF() extends Token final case class IMPLEMENTS() extends Token final case class IMPORT() extends Token + final case class IN() extends Token final case class INCLUDE() extends Token final case class INITIAL() extends Token final case class INPUT() extends Token @@ -85,6 +86,7 @@ object Token { final case class MACHINE() extends Token final case class MATCH() extends Token final case class MINUS() extends Token + final case class MINUS_MINUS() extends Token final case class MODULE() extends Token final case class OMIT() extends Token final case class ON() extends Token @@ -222,6 +224,7 @@ enum TokenId { case IF case IMPLEMENTS case IMPORT + case IN case INCLUDE case INITIAL case INPUT @@ -297,6 +300,7 @@ enum TokenId { case LBRACKET case LPAREN case MINUS + case MINUS_MINUS case PLUS case RARROW case RBRACE diff --git a/compiler/lib/src/main/scala/transform/ExpandTemplates.scala b/compiler/lib/src/main/scala/transform/ExpandTemplates.scala index e04b68dd3..9c1efaed2 100644 --- a/compiler/lib/src/main/scala/transform/ExpandTemplates.scala +++ b/compiler/lib/src/main/scala/transform/ExpandTemplates.scala @@ -114,6 +114,32 @@ object ExpandTemplates extends AstUnitTransformer Right((), cloneNode(a, node, Ast.TypeNameQualIdent(qidNode))) } + override def typeNameRangeNode( + a: Analysis, + node: AstNode[Ast.TypeName], + tn: Ast.TypeNameRange + ) = + def cloneExprOpt(eOpt: Option[AstNode[Ast.Expr]]) = + eOpt match { + case None => Right(None) + case Some(e) => + for ((_, e) <- matchExprNode(a, e)) yield Some(e) + } + def cloneBound(bound: Ast.TypeNameRange.Bound) = + val (kind, eOpt) = bound + for (eOpt <- cloneExprOpt(eOpt)) + yield (kind, eOpt) + for { + (_, typeName) <- matchTypeName(a, tn.typeName) + lowerBound <- cloneBound(tn.lowerBound) + upperBound <- cloneBound(tn.upperBound) + } + yield { + val tn1 = Ast.TypeNameRange(typeName, lowerBound, upperBound) + val node1 = cloneNode(a, node, tn1) + ((), node1) + } + override def typeNameStringNode( a: Analysis, node: AstNode[Ast.TypeName], diff --git a/compiler/lib/src/test/input/syntax/lexer/ok/reserved-words.fpp b/compiler/lib/src/test/input/syntax/lexer/ok/reserved-words.fpp index d587c59a3..1e298bb5f 100644 --- a/compiler/lib/src/test/input/syntax/lexer/ok/reserved-words.fpp +++ b/compiler/lib/src/test/input/syntax/lexer/ok/reserved-words.fpp @@ -39,6 +39,7 @@ health high id import +in include input instance diff --git a/compiler/lib/src/test/input/syntax/lexer/ok/symbols.fpp b/compiler/lib/src/test/input/syntax/lexer/ok/symbols.fpp index ae9b65b01..bcdf076bf 100644 --- a/compiler/lib/src/test/input/syntax/lexer/ok/symbols.fpp +++ b/compiler/lib/src/test/input/syntax/lexer/ok/symbols.fpp @@ -2,6 +2,7 @@ ) , - +-- -> . : diff --git a/compiler/lib/src/test/scala/syntax/Lexer.scala b/compiler/lib/src/test/scala/syntax/Lexer.scala index bb3d1280a..7ab932625 100644 --- a/compiler/lib/src/test/scala/syntax/Lexer.scala +++ b/compiler/lib/src/test/scala/syntax/Lexer.scala @@ -130,6 +130,56 @@ class LexerSpec extends AnyWordSpec { } } + "range syntax" should { + def lex(s: String) = + Lexer.Scanner( + FppFile.StdIn, + s.toCharArray + )(using Context()).list() + "lex \"in\" to IN" in { + lex("in") match { + case Right(Token.IN() :: List()) => () + case _ => assert(false) + } + } + "lex \"$in\" to an identifier" in { + lex("$in") match { + case Right(Token.IDENTIFIER("in") :: List()) => () + case _ => assert(false) + } + } + "lex \"--\" to MINUS_MINUS" in { + lex("--") match { + case Right(Token.MINUS_MINUS() :: List()) => () + case _ => assert(false) + } + } + "lex \"- -\" to two MINUS tokens" in { + lex("- -") match { + case Right(Token.MINUS() :: Token.MINUS() :: List()) => () + case _ => assert(false) + } + } + "lex \"--->\" to MINUS_MINUS followed by RARROW" in { + lex("--->") match { + case Right(Token.MINUS_MINUS() :: Token.RARROW() :: List()) => () + case _ => assert(false) + } + } + "lex \"---\" to MINUS_MINUS followed by MINUS" in { + lex("---") match { + case Right(Token.MINUS_MINUS() :: Token.MINUS() :: List()) => () + case _ => assert(false) + } + } + "not suppress a newline after \"--\"" in { + lex("--\n") match { + case Right(Token.MINUS_MINUS() :: Token.EOL() :: List()) => () + case _ => assert(false) + } + } + } + "string literal" should { def lex(s: String): Unit = { Lexer.Scanner( diff --git a/compiler/lib/src/test/scala/syntax/Parser.scala b/compiler/lib/src/test/scala/syntax/Parser.scala index 9e0400f38..578cfa63d 100644 --- a/compiler/lib/src/test/scala/syntax/Parser.scala +++ b/compiler/lib/src/test/scala/syntax/Parser.scala @@ -64,7 +64,9 @@ class ParserSpec extends AnyWordSpec { List( "type a = U32", "type a = F32", - "dictionary type a = string" + "dictionary type a = string", + "type a = U8 in [0, 10]", + "dictionary type a = F32 in (--, 10.5)" ) ) } @@ -76,7 +78,8 @@ class ParserSpec extends AnyWordSpec { "array a = [10] U32", "array a = [10] U32 default 0", "array a = [10] U32 default 0 format \"{} counts\"", - "dictionary array a = [10] U32" + "dictionary array a = [10] U32", + "array a = [10] U32 in [1, 5] default 1" ) ) } @@ -368,7 +371,9 @@ class ParserSpec extends AnyWordSpec { }""", "sizeof(U32)", "sizeof(string size 10)", - "sizeof(a.b.c)" + "sizeof(a.b.c)", + "sizeof(U32 in [0, 10])", + "1 - -1", ) ) } @@ -380,6 +385,9 @@ class ParserSpec extends AnyWordSpec { "@", "a.", "bool", + // -- is a single token, not two minus signs + "--1", + "1--1", ) ) } @@ -490,6 +498,17 @@ class ParserSpec extends AnyWordSpec { "connections C {}", "connections C { a.b -> c.d }", "connections C { a.b -> c.d, e.f -> g.h }", + "connections C { a.out -> c.$in }", + ) + ) + } + + "spec connection graph direct error" should { + parseAllError( + Parser.specConnectionGraph, + List( + // in is a reserved word + "connections C { a.out -> c.in }", ) ) } @@ -754,6 +773,17 @@ class ParserSpec extends AnyWordSpec { List( "port a = a.b.c.d", "port b = c.b", + "port $in = c.b", + ) + ) + } + + "spec topology port error" should { + parseAllError( + Parser.specTopPort, + List( + // in is a reserved word + "port in = c.b", ) ) } @@ -789,10 +819,79 @@ class ParserSpec extends AnyWordSpec { "a", "a.b", "a.b.c", + "U8 in [0, 10]", + "U8 in (0, 10)", + "U8 in [0, 10)", + "U8 in (0, 10]", + "I16 in (--, 10)", + "I16 in [--, 10)", + "U16 in [0, --)", + "U16 in [0, --]", + "U16 in [--, --]", + "F32 in [-1.5, 10.5)", + "U32 in [a, a + b * 2]", + "U32 in [(0), sizeof(U8)]", + "a.b in [0, 10]", + ".a.b in [0, 10]", + "T in [0, --] in (--, 5]", + """U8 in [ + 0, + 10 + ]""", + """U8 in ( + --, + 10 + )""", ) ) } + "type name range AST" should { + import Ast.TypeNameRange.BoundKind.{EXCLUSIVE, INCLUSIVE} + "represent bounds and bound kinds" in { + val Right(tn) = Parser.parseString(Parser.typeName)("I16 in (--, 10]").runtimeChecked + tn match { + case Ast.TypeNameRange( + AstNode(Ast.TypeNameInt(Ast.I16), _), + (EXCLUSIVE, None), + (INCLUSIVE, Some(AstNode(Ast.ExprLiteralInt("10"), _))) + ) => () + case _ => fail(s"unexpected AST $tn") + } + } + "associate chained ranges to the left" in { + val Right(tn) = Parser.parseString(Parser.typeName)("T in [0, --] in (--, 5)").runtimeChecked + tn match { + case Ast.TypeNameRange( + AstNode( + Ast.TypeNameRange( + AstNode(Ast.TypeNameQualIdent(AstNode(Ast.QualIdent.Unqualified("T", false), _)), _), + (INCLUSIVE, Some(AstNode(Ast.ExprLiteralInt("0"), _))), + (INCLUSIVE, None) + ), + _ + ), + (EXCLUSIVE, None), + (EXCLUSIVE, Some(AstNode(Ast.ExprLiteralInt("5"), _))) + ) => () + case _ => fail(s"unexpected AST $tn") + } + } + "record locations for the type names inside ranges" in { + val Right(tn) = Parser.parseString(Parser.typeName)(" U8 in [0, 1] in [0, 1]").runtimeChecked + def checkLoc(node: AstNode[Ast.TypeName]) = { + val pos = Locations.get(node.id).pos + assert(pos.line == 1 && pos.column == 3) + } + tn match { + case Ast.TypeNameRange(outer @ AstNode(Ast.TypeNameRange(inner, _, _), _), _, _) => + checkLoc(outer) + checkLoc(inner) + case _ => fail(s"unexpected AST $tn") + } + } + } + "template ok" should { parseAllOK( Parser.transUnit, @@ -984,6 +1083,33 @@ class ParserSpec extends AnyWordSpec { ) } + "type name range format round trip OK" should { + roundTripAllOK( + List( + "type T = U8 in [0, 10]", + "type T = U8 in (0, 10)", + "type T = U8 in [0, 10)", + "type T = U8 in (0, 10]", + "type T = I16 in (--, 10)", + "type T = I16 in [--, 10)", + "type T = U16 in [0, --)", + "type T = U16 in [0, --]", + "type T = U16 in [--, --]", + "type T = F32 in [-1.5, 10.5)", + "type T = U32 in [a, a + b * 2]", + "type T = U32 in [(0), sizeof(U8 in [0, 1])]", + "type T = .a.b in [0, 10]", + "type T = $in in [0, 10]", + "type T = S in [0, --] in (--, 5]", + "dictionary type T = U8 in [0, 10]", + "array A = [3] U32 in [1, 5] default 1", + "struct S { x: U32 in [0, 10] }", + "constant c = sizeof(I8 in (--, 0))", + "port P(x: U8 in [0, 10]) -> F64 in (0, --)", + ) + ) + } + "trans unit OK" should { parseAllOK( Parser.transUnit, @@ -1046,6 +1172,27 @@ class ParserSpec extends AnyWordSpec { "@", "a.", "1+3", + // Missing range + "U8 in", + // Missing brackets or parentheses + "U8 in 0, 10", + "U8 in [0, 10", + "U8 in 0, 10]", + // Wrong brackets + "U8 in {0, 10}", + // Missing comma + "U8 in [0 10]", + // Wrong number of bounds + "U8 in []", + "U8 in [0]", + "U8 in [0, ]", + "U8 in [, 10]", + "U8 in [0, 1, 2]", + // Bound is not an expression or -- + "U8 in [-, 10]", + "U8 in [0, U8]", + // in is a reserved word + "in", ) ) } diff --git a/compiler/tools/fpp-check/test/connection_direct/invalid_port_instance.fpp b/compiler/tools/fpp-check/test/connection_direct/invalid_port_instance.fpp index 2a5e9762b..ec11abedc 100644 --- a/compiler/tools/fpp-check/test/connection_direct/invalid_port_instance.fpp +++ b/compiler/tools/fpp-check/test/connection_direct/invalid_port_instance.fpp @@ -3,6 +3,6 @@ passive component C { instance c: C base id 0x100 topology T { connections C { - c.out -> c.in + c.out -> c.$in } } diff --git a/compiler/tools/fpp-check/test/connection_direct/invalid_port_instance.ref.txt b/compiler/tools/fpp-check/test/connection_direct/invalid_port_instance.ref.txt index eb2ab572e..53dacfb62 100644 --- a/compiler/tools/fpp-check/test/connection_direct/invalid_port_instance.ref.txt +++ b/compiler/tools/fpp-check/test/connection_direct/invalid_port_instance.ref.txt @@ -1,5 +1,5 @@ fpp-check [ local path prefix ]/compiler/tools/fpp-check/test/connection_direct/invalid_port_instance.fpp:6.7 - c.out -> c.in + c.out -> c.$in ^ error: out is not a port instance of component c diff --git a/compiler/tools/fpp-check/test/connection_direct/undef_instance.fpp b/compiler/tools/fpp-check/test/connection_direct/undef_instance.fpp index ea3f76fbf..6b0636a89 100644 --- a/compiler/tools/fpp-check/test/connection_direct/undef_instance.fpp +++ b/compiler/tools/fpp-check/test/connection_direct/undef_instance.fpp @@ -1,5 +1,5 @@ topology T { connections C { - c.out -> c.in + c.out -> c.$in } } diff --git a/compiler/tools/fpp-check/test/connection_direct/undef_instance.ref.txt b/compiler/tools/fpp-check/test/connection_direct/undef_instance.ref.txt index 637d96bc9..030812259 100644 --- a/compiler/tools/fpp-check/test/connection_direct/undef_instance.ref.txt +++ b/compiler/tools/fpp-check/test/connection_direct/undef_instance.ref.txt @@ -1,6 +1,6 @@ fpp-check [ local path prefix ]/compiler/tools/fpp-check/test/connection_direct/undef_instance.fpp:3.5 - c.out -> c.in + c.out -> c.$in ^ error: symbol c is not defined note: looking for a component instance or topology here diff --git a/compiler/tools/fpp-check/test/top_ports/nested.fpp b/compiler/tools/fpp-check/test/top_ports/nested.fpp index 56ac92838..6085a7cb3 100644 --- a/compiler/tools/fpp-check/test/top_ports/nested.fpp +++ b/compiler/tools/fpp-check/test/top_ports/nested.fpp @@ -20,7 +20,7 @@ topology A { instance c2 port out = c1.pOut - port in = c2.pIn + port $in = c2.pIn } topology B { @@ -36,7 +36,7 @@ topology C { instance B connections P { - B.out -> A.in + B.out -> A.$in } } diff --git a/compiler/tools/fpp-depend/test/spec_connection_graph_direct.fpp b/compiler/tools/fpp-depend/test/spec_connection_graph_direct.fpp index 655993112..945e5dcab 100644 --- a/compiler/tools/fpp-depend/test/spec_connection_graph_direct.fpp +++ b/compiler/tools/fpp-depend/test/spec_connection_graph_direct.fpp @@ -7,7 +7,7 @@ topology T { connections C { - a.out[b] -> c.in[d] + a.out[b] -> c.$in[d] } diff --git a/compiler/tools/fpp-syntax/test/syntax-ast.ref.txt b/compiler/tools/fpp-syntax/test/syntax-ast.ref.txt index e389ac735..4e726ea50 100644 --- a/compiler/tools/fpp-syntax/test/syntax-ast.ref.txt +++ b/compiler/tools/fpp-syntax/test/syntax-ast.ref.txt @@ -516,31 +516,116 @@ def module @ Type names def module ident TypeNames - def array + def alias type ident typeNameU32 - size literal int 10 type name U32 - def array + def alias type ident typeNameF32 - size literal int 10 type name F32 - def array + def alias type ident typeNameBool - size literal int 10 type name bool - def array + def alias type ident typeNameString - size literal int 10 type name string size literal int 256 - def array + def alias type ident typeNameQID - size literal int 10 type name qual ident a.b.c - def array + def alias type ident typeNameAbsoluteQID - size literal int 10 type name qual ident .a.b.c + @ Range type names + def module + ident Range + def alias type + ident inclusiveInclusive + type name range + type name U8 + inclusive lower bound literal int 0 + inclusive upper bound literal int 10 + def alias type + ident exclusiveExclusive + type name range + type name U8 + exclusive lower bound literal int 0 + exclusive upper bound literal int 10 + def alias type + ident inclusiveExclusive + type name range + type name U8 + inclusive lower bound literal int 0 + exclusive upper bound literal int 10 + def alias type + ident exclusiveInclusive + type name range + type name U8 + exclusive lower bound literal int 0 + inclusive upper bound literal int 10 + def alias type + ident noLowerBound + type name range + type name I16 + exclusive lower bound -- + exclusive upper bound literal int 10 + def alias type + ident noUpperBound + type name range + type name U16 + inclusive lower bound literal int 0 + inclusive upper bound -- + def alias type + ident noBounds + type name range + type name U16 + inclusive lower bound -- + inclusive upper bound -- + def alias type + ident floatBounds + type name range + type name F32 + inclusive lower bound expr unop + unop - + literal float 1.5 + exclusive upper bound literal float 10.5 + def alias type + ident exprBounds + type name range + type name U32 + inclusive lower bound expr dot + ident a + ident b + inclusive upper bound expr binop + expr dot + ident a + ident b + binop + + expr binop + literal int 2 + binop * + ident c + def alias type + ident qualIdent + type name range + type name qual ident a.b.c + inclusive lower bound literal int 0 + inclusive upper bound literal int 10 + def alias type + ident chained + type name range + type name range + type name qual ident a.b.c + inclusive lower bound literal int 0 + inclusive upper bound -- + exclusive lower bound -- + inclusive upper bound literal int 5 + def alias type + ident multiLine + type name range + type name U8 + inclusive lower bound literal int 0 + inclusive upper bound literal int 10 + @< Range type names @< Type names @ Expressions def module @@ -724,5 +809,12 @@ def module expr sizeof type name string size literal int 20 + def constant + ident typeRange + expr sizeof + type name range + type name U8 + inclusive lower bound literal int 0 + inclusive upper bound literal int 10 @< SizeOf @< Expressions diff --git a/compiler/tools/fpp-syntax/test/syntax-include-ast.ref.txt b/compiler/tools/fpp-syntax/test/syntax-include-ast.ref.txt index 23c24dc68..e065bb903 100644 --- a/compiler/tools/fpp-syntax/test/syntax-include-ast.ref.txt +++ b/compiler/tools/fpp-syntax/test/syntax-include-ast.ref.txt @@ -526,31 +526,116 @@ def module @ Type names def module ident TypeNames - def array + def alias type ident typeNameU32 - size literal int 10 type name U32 - def array + def alias type ident typeNameF32 - size literal int 10 type name F32 - def array + def alias type ident typeNameBool - size literal int 10 type name bool - def array + def alias type ident typeNameString - size literal int 10 type name string size literal int 256 - def array + def alias type ident typeNameQID - size literal int 10 type name qual ident a.b.c - def array + def alias type ident typeNameAbsoluteQID - size literal int 10 type name qual ident .a.b.c + @ Range type names + def module + ident Range + def alias type + ident inclusiveInclusive + type name range + type name U8 + inclusive lower bound literal int 0 + inclusive upper bound literal int 10 + def alias type + ident exclusiveExclusive + type name range + type name U8 + exclusive lower bound literal int 0 + exclusive upper bound literal int 10 + def alias type + ident inclusiveExclusive + type name range + type name U8 + inclusive lower bound literal int 0 + exclusive upper bound literal int 10 + def alias type + ident exclusiveInclusive + type name range + type name U8 + exclusive lower bound literal int 0 + inclusive upper bound literal int 10 + def alias type + ident noLowerBound + type name range + type name I16 + exclusive lower bound -- + exclusive upper bound literal int 10 + def alias type + ident noUpperBound + type name range + type name U16 + inclusive lower bound literal int 0 + inclusive upper bound -- + def alias type + ident noBounds + type name range + type name U16 + inclusive lower bound -- + inclusive upper bound -- + def alias type + ident floatBounds + type name range + type name F32 + inclusive lower bound expr unop + unop - + literal float 1.5 + exclusive upper bound literal float 10.5 + def alias type + ident exprBounds + type name range + type name U32 + inclusive lower bound expr dot + ident a + ident b + inclusive upper bound expr binop + expr dot + ident a + ident b + binop + + expr binop + literal int 2 + binop * + ident c + def alias type + ident qualIdent + type name range + type name qual ident a.b.c + inclusive lower bound literal int 0 + inclusive upper bound literal int 10 + def alias type + ident chained + type name range + type name range + type name qual ident a.b.c + inclusive lower bound literal int 0 + inclusive upper bound -- + exclusive lower bound -- + inclusive upper bound literal int 5 + def alias type + ident multiLine + type name range + type name U8 + inclusive lower bound literal int 0 + inclusive upper bound literal int 10 + @< Range type names @< Type names @ Expressions def module @@ -734,5 +819,12 @@ def module expr sizeof type name string size literal int 20 + def constant + ident typeRange + expr sizeof + type name range + type name U8 + inclusive lower bound literal int 0 + inclusive upper bound literal int 10 @< SizeOf @< Expressions diff --git a/compiler/tools/fpp-syntax/test/syntax-stdin.ref.txt b/compiler/tools/fpp-syntax/test/syntax-stdin.ref.txt index 23c24dc68..e065bb903 100644 --- a/compiler/tools/fpp-syntax/test/syntax-stdin.ref.txt +++ b/compiler/tools/fpp-syntax/test/syntax-stdin.ref.txt @@ -526,31 +526,116 @@ def module @ Type names def module ident TypeNames - def array + def alias type ident typeNameU32 - size literal int 10 type name U32 - def array + def alias type ident typeNameF32 - size literal int 10 type name F32 - def array + def alias type ident typeNameBool - size literal int 10 type name bool - def array + def alias type ident typeNameString - size literal int 10 type name string size literal int 256 - def array + def alias type ident typeNameQID - size literal int 10 type name qual ident a.b.c - def array + def alias type ident typeNameAbsoluteQID - size literal int 10 type name qual ident .a.b.c + @ Range type names + def module + ident Range + def alias type + ident inclusiveInclusive + type name range + type name U8 + inclusive lower bound literal int 0 + inclusive upper bound literal int 10 + def alias type + ident exclusiveExclusive + type name range + type name U8 + exclusive lower bound literal int 0 + exclusive upper bound literal int 10 + def alias type + ident inclusiveExclusive + type name range + type name U8 + inclusive lower bound literal int 0 + exclusive upper bound literal int 10 + def alias type + ident exclusiveInclusive + type name range + type name U8 + exclusive lower bound literal int 0 + inclusive upper bound literal int 10 + def alias type + ident noLowerBound + type name range + type name I16 + exclusive lower bound -- + exclusive upper bound literal int 10 + def alias type + ident noUpperBound + type name range + type name U16 + inclusive lower bound literal int 0 + inclusive upper bound -- + def alias type + ident noBounds + type name range + type name U16 + inclusive lower bound -- + inclusive upper bound -- + def alias type + ident floatBounds + type name range + type name F32 + inclusive lower bound expr unop + unop - + literal float 1.5 + exclusive upper bound literal float 10.5 + def alias type + ident exprBounds + type name range + type name U32 + inclusive lower bound expr dot + ident a + ident b + inclusive upper bound expr binop + expr dot + ident a + ident b + binop + + expr binop + literal int 2 + binop * + ident c + def alias type + ident qualIdent + type name range + type name qual ident a.b.c + inclusive lower bound literal int 0 + inclusive upper bound literal int 10 + def alias type + ident chained + type name range + type name range + type name qual ident a.b.c + inclusive lower bound literal int 0 + inclusive upper bound -- + exclusive lower bound -- + inclusive upper bound literal int 5 + def alias type + ident multiLine + type name range + type name U8 + inclusive lower bound literal int 0 + inclusive upper bound literal int 10 + @< Range type names @< Type names @ Expressions def module @@ -734,5 +819,12 @@ def module expr sizeof type name string size literal int 20 + def constant + ident typeRange + expr sizeof + type name range + type name U8 + inclusive lower bound literal int 0 + inclusive upper bound literal int 10 @< SizeOf @< Expressions diff --git a/compiler/tools/fpp-syntax/test/syntax.fpp b/compiler/tools/fpp-syntax/test/syntax.fpp index bca7a615a..2b92c3d52 100644 --- a/compiler/tools/fpp-syntax/test/syntax.fpp +++ b/compiler/tools/fpp-syntax/test/syntax.fpp @@ -308,12 +308,32 @@ module DefinitionsAndSpecifiers { @ Type names module TypeNames { - array typeNameU32 = [10] U32 - array typeNameF32 = [10] F32 - array typeNameBool = [10] bool - array typeNameString = [10] string size 256 - array typeNameQID = [10] a.b.c - array typeNameAbsoluteQID = [10] .a.b.c + type typeNameU32 = U32 + type typeNameF32 = F32 + type typeNameBool = bool + type typeNameString = string size 256 + type typeNameQID = a.b.c + type typeNameAbsoluteQID = .a.b.c + + @ Range type names + module Range { + type inclusiveInclusive = U8 in [0, 10] + type exclusiveExclusive = U8 in (0, 10) + type inclusiveExclusive = U8 in [0, 10) + type exclusiveInclusive = U8 in (0, 10] + type noLowerBound = I16 in (--, 10) + type noUpperBound = U16 in [0, --] + type noBounds = U16 in [--, --] + type floatBounds = F32 in [-1.5, 10.5) + type exprBounds = U32 in [a.b, a.b + 2 * c] + type qualIdent = a.b.c in [0, 10] + type chained = a.b.c in [0, --] in (--, 5] + type multiLine = U8 in [ + 0, + 10 + ] + } + @< Range type names } @< Type names @@ -398,6 +418,7 @@ module Expressions { constant typeFloat = sizeof(F64) constant typeQualId = sizeof(TypeNames.typeNameU32) constant typeString = sizeof(string size 20) + constant typeRange = sizeof(U8 in [0, 10]) } @< SizeOf diff --git a/compiler/tools/fpp-to-cpp/test/top/ports/topology.fpp b/compiler/tools/fpp-to-cpp/test/top/ports/topology.fpp index 781985f45..7ce3512e0 100644 --- a/compiler/tools/fpp-to-cpp/test/top/ports/topology.fpp +++ b/compiler/tools/fpp-to-cpp/test/top/ports/topology.fpp @@ -70,7 +70,7 @@ module M { } port out = passive2.p - port in = active2.p + port $in = active2.p } @@ -80,7 +80,7 @@ module M { connections C2 { - Ports1.out -> Ports1.in + Ports1.out -> Ports1.$in } diff --git a/compiler/tools/fpp-to-fpp/test/include.ref.txt b/compiler/tools/fpp-to-fpp/test/include.ref.txt index db244390b..b88056715 100644 --- a/compiler/tools/fpp-to-fpp/test/include.ref.txt +++ b/compiler/tools/fpp-to-fpp/test/include.ref.txt @@ -350,17 +350,46 @@ module DefinitionsAndSpecifiers { @ Type names module TypeNames { - array typeNameU32 = [10] U32 + type typeNameU32 = U32 - array typeNameF32 = [10] F32 + type typeNameF32 = F32 - array typeNameBool = [10] bool + type typeNameBool = bool - array typeNameString = [10] string size 256 + type typeNameString = string size 256 - array typeNameQID = [10] a.b.c + type typeNameQID = a.b.c - array typeNameAbsoluteQID = [10] .a.b.c + type typeNameAbsoluteQID = .a.b.c + + @ Range type names + module Range { + + type inclusiveInclusive = U8 in [0, 10] + + type exclusiveExclusive = U8 in (0, 10) + + type inclusiveExclusive = U8 in [0, 10) + + type exclusiveInclusive = U8 in (0, 10] + + type noLowerBound = I16 in (--, 10) + + type noUpperBound = U16 in [0, --] + + type noBounds = U16 in [--, --] + + type floatBounds = F32 in [-1.5, 10.5) + + type exprBounds = U32 in [a.b, a.b + 2 * c] + + type qualIdent = a.b.c in [0, 10] + + type chained = a.b.c in [0, --] in (--, 5] + + type multiLine = U8 in [0, 10] + + } @< Range type names } @< Type names @@ -443,6 +472,8 @@ module Expressions { constant typeString = sizeof(string size 20) + constant typeRange = sizeof(U8 in [0, 10]) + } @< SizeOf } @< Expressions diff --git a/compiler/tools/fpp-to-fpp/test/no_include.ref.txt b/compiler/tools/fpp-to-fpp/test/no_include.ref.txt index f6b80c7a4..62601aef0 100644 --- a/compiler/tools/fpp-to-fpp/test/no_include.ref.txt +++ b/compiler/tools/fpp-to-fpp/test/no_include.ref.txt @@ -343,17 +343,46 @@ module DefinitionsAndSpecifiers { @ Type names module TypeNames { - array typeNameU32 = [10] U32 + type typeNameU32 = U32 - array typeNameF32 = [10] F32 + type typeNameF32 = F32 - array typeNameBool = [10] bool + type typeNameBool = bool - array typeNameString = [10] string size 256 + type typeNameString = string size 256 - array typeNameQID = [10] a.b.c + type typeNameQID = a.b.c - array typeNameAbsoluteQID = [10] .a.b.c + type typeNameAbsoluteQID = .a.b.c + + @ Range type names + module Range { + + type inclusiveInclusive = U8 in [0, 10] + + type exclusiveExclusive = U8 in (0, 10) + + type inclusiveExclusive = U8 in [0, 10) + + type exclusiveInclusive = U8 in (0, 10] + + type noLowerBound = I16 in (--, 10) + + type noUpperBound = U16 in [0, --] + + type noBounds = U16 in [--, --] + + type floatBounds = F32 in [-1.5, 10.5) + + type exprBounds = U32 in [a.b, a.b + 2 * c] + + type qualIdent = a.b.c in [0, 10] + + type chained = a.b.c in [0, --] in (--, 5] + + type multiLine = U8 in [0, 10] + + } @< Range type names } @< Type names @@ -436,6 +465,8 @@ module Expressions { constant typeString = sizeof(string size 20) + constant typeRange = sizeof(U8 in [0, 10]) + } @< SizeOf } @< Expressions diff --git a/docs/fpp-spec.html b/docs/fpp-spec.html index 933152493..592d08c62 100644 --- a/docs/fpp-spec.html +++ b/docs/fpp-spec.html @@ -1393,6 +1393,7 @@