Sfoglia il codice sorgente

Adding bytes as a type

Inte Vleminckx 1 anno fa
parent
commit
98f36c4cf0
3 ha cambiato i file con 10 aggiunte e 4 eliminazioni
  1. 5 0
      concrete_syntax/common.py
  2. 3 2
      state/base.py
  3. 2 2
      transformation/merger.py

+ 5 - 0
concrete_syntax/common.py

@@ -16,6 +16,8 @@ def display_value(val: any, type_name: str, indentation=0, newline_character='\n
         return '"'+val+'"'.replace('\n', newline_character)
     elif type_name == "Integer" or type_name == "Boolean":
         return str(val)
+    elif type_name == "Bytes":
+        return val
     else:
         raise Exception("don't know how to display value" + type_name)
 
@@ -48,6 +50,9 @@ class TBase(Transformer):
     def CODE(self, token):
         return _Code(str(token[1:-1])) # strip the ``
 
+    def BYTES(self, token):
+        return (bytes(token[2:-1], "utf-8"), token.line)  # Strip b"" or b''
+
     def INDENTED_CODE(self, token):
         skip = 4 # strip the ``` and the following newline character
         space_count = 0

+ 3 - 2
state/base.py

@@ -2,13 +2,14 @@ from abc import ABC, abstractmethod
 from typing import Any, List, Tuple, Optional, Union
 from uuid import UUID, uuid4
 
-primitive_types = (int, float, str, bool)
+primitive_types = (int, float, str, bool, bytes)
 INTEGER = ("Integer",)
 FLOAT = ("Float",)
 STRING = ("String",)
 BOOLEAN = ("Boolean",)
 TYPE = ("Type",)
-type_values = (INTEGER, FLOAT, STRING, BOOLEAN, TYPE)
+BYTES = ("Bytes",)
+type_values = (INTEGER, FLOAT, STRING, BOOLEAN, TYPE, BYTES)
 
 
 Node = UUID

+ 2 - 2
transformation/merger.py

@@ -4,7 +4,7 @@ from concrete_syntax.textual_od import parser, renderer
 from services.scd import SCD
 from util.timer import Timer
 
-PRIMITIVE_TYPES = set(["Integer", "String", "Boolean", "ActionCode"])
+PRIMITIVE_TYPES = set(["Integer", "String", "Boolean", "ActionCode", "Bytes"])
 
 # Merges N models. The models must have the same meta-model.
 # Care should be taken to avoid naming collisions before calling this function.
@@ -12,7 +12,7 @@ def merge_models(state, mm, models: list[UUID]):
     with Timer("merge_models"):
         primitive_types = {
             type_name : UUID(state.read_value(state.read_dict(state.read_root(), type_name)))
-                for type_name in ["Integer", "String", "Boolean", "ActionCode"]
+                for type_name in ["Integer", "String", "Boolean", "ActionCode", "Bytes"]
         }
 
         merged = state.create_node()