From cac7dd6bec1f313dec642d9a4c288b66192f2384 Mon Sep 17 00:00:00 2001 From: Emmanuel Briot Date: Fri, 21 Oct 2022 15:15:57 +0200 Subject: [PATCH] Added support for floating point types --- corpus/types.txt | 137 ++++++++++++++++++++++++++++++++++++++++++++++- grammar.js | 47 +++++++++++++--- 2 files changed, 176 insertions(+), 8 deletions(-) diff --git a/corpus/types.txt b/corpus/types.txt index e57caed..675a8b2 100644 --- a/corpus/types.txt +++ b/corpus/types.txt @@ -37,7 +37,8 @@ Derived type ================================================================================ package P is - type B is new Integer; + type B is new Integer + with Size => 8; end P; -------------------------------------------------------------------------------- @@ -54,6 +55,138 @@ end P; (derived_type_definition (subtype_indication (name - (identifier))))))) + (identifier))))) + (aspect_specification + (aspect_mark_list + (aspect_association + (aspect_mark + (identifier)) + (aspect_definition + (expression + (relation + (simple_expression + (term + (factor + (primary + (numeric_literal))))))))))))) + (name + (identifier))))) + +================================================================================ +Modular types +================================================================================ + +package P is + type C is mod 256; +end P; + +-------------------------------------------------------------------------------- + +(compilation + (compilation_unit + (package_specification + (name + (identifier)) + (type_declaration + (full_type_declaration + (identifier) + (type_definition + (integer_type_definition + (modular_type_definition + (expression + (relation + (simple_expression + (term + (factor + (primary + (numeric_literal)))))))))))) + (name + (identifier))))) + +================================================================================ +Fixed points +================================================================================ + +package P is + type B is new Float range 0.0 .. 1.0; + type D is delta 0.1 digits 8; + type E is delta 0.1 range 0.0 .. 1.0; +end P; + +-------------------------------------------------------------------------------- + +(compilation + (compilation_unit + (package_specification + (name + (identifier)) + (type_declaration + (full_type_declaration + (identifier) + (type_definition + (derived_type_definition + (subtype_indication + (name + (identifier)) + (constraint + (scalar_constraint + (range_constraint + (range_g + (simple_expression + (term + (factor + (primary + (numeric_literal))))) + (simple_expression + (term + (factor + (primary + (numeric_literal)))))))))))))) + (type_declaration + (full_type_declaration + (identifier) + (type_definition + (real_type_definition + (fixed_point_definition + (decimal_fixed_point_definition + (expression + (relation + (simple_expression + (term + (factor + (primary + (numeric_literal))))))) + (expression + (relation + (simple_expression + (term + (factor + (primary + (numeric_literal))))))))))))) + (type_declaration + (full_type_declaration + (identifier) + (type_definition + (real_type_definition + (fixed_point_definition + (ordinary_fixed_point_definition + (expression + (relation + (simple_expression + (term + (factor + (primary + (numeric_literal))))))) + (real_range_specification + (simple_expression + (term + (factor + (primary + (numeric_literal))))) + (simple_expression + (term + (factor + (primary + (numeric_literal)))))))))))) (name (identifier))))) diff --git a/grammar.js b/grammar.js index 9214dd7..f5c2e22 100644 --- a/grammar.js +++ b/grammar.js @@ -2,7 +2,7 @@ * A case-insensitive keyword (copied from VHDL grammar) */ const reservedWord = word => - // word || // when debugging conflict error msgs + //word || // when debugging conflict error msgs alias(reserved(caseInsensitive(word)), word) ; const reserved = regex => token(prec(2, new RegExp(regex))); @@ -568,7 +568,7 @@ module.exports = grammar({ // optional($.known_discriminant_part), reservedWord('is'), $.type_definition, -// optional($.aspect_specification), + optional($.aspect_specification), ';', ), // $.task_type_declaration, @@ -577,7 +577,7 @@ module.exports = grammar({ type_definition: $ => choice( // $.enumeration_type_definition, $.integer_type_definition, -// $.real_type_definition, + $.real_type_definition, // $.array_type_definition, // $.record_type_definition, // $.access_type_definition, @@ -586,7 +586,42 @@ module.exports = grammar({ ), integer_type_definition: $ => choice( $.signed_integer_type_definition, -// $.modular_type_definition, + $.modular_type_definition, + ), + modular_type_definition: $ => seq( + reservedWord('mod'), + $.expression, + ), + real_type_definition: $ => choice( + $.floating_point_definition, + $.fixed_point_definition, + ), + floating_point_definition: $ => seq( + reservedWord('digits'), + $.expression, + optional($.real_range_specification), + ), + real_range_specification: $ => seq( + reservedWord('range'), + $.simple_expression, + '..', + $.simple_expression, + ), + fixed_point_definition: $ => choice( + $.ordinary_fixed_point_definition, + $.decimal_fixed_point_definition, + ), + decimal_fixed_point_definition: $ => seq( + reservedWord('delta'), + $.expression, + reservedWord('digits'), + $.expression, + optional($.real_range_specification), + ), + ordinary_fixed_point_definition: $ => seq( + reservedWord('delta'), + $.expression, + $.real_range_specification, ), signed_integer_type_definition: $ => seq( reservedWord('range'), @@ -594,7 +629,7 @@ module.exports = grammar({ '..', $.simple_expression, ), - derived_type_definition: $ => seq( + derived_type_definition: $ => prec.left(seq( optional(reservedWord('abstract')), optional(reservedWord('limited')), reservedWord('new'), @@ -606,7 +641,7 @@ module.exports = grammar({ // )), $.record_extension_part, )), - ), + )), record_extension_part: $ => seq( reservedWord('with'), $.record_definition,