diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-03-22 09:54:27 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-03-22 10:16:02 +0000 |
| commit | 47442811098a6ed4d8a6ae80f06c13f47451a4d9 (patch) | |
| tree | 4e7549a4816ed082719da49d7ebab10cb91f9b22 | |
| parent | 6e2425bfc5ad9bfac68289a9dcc2cccee1441fd9 (diff) | |
Add flatbuffers schema
| -rw-r--r-- | flatbuffers/patio.fbs | 484 | ||||
| -rw-r--r-- | flatbuffers/sources.fbs | 25 | ||||
| -rw-r--r-- | generate-flatbuffers.py | 56 | ||||
| -rw-r--r-- | makefile | 15 | ||||
| -rw-r--r-- | pom.xml | 5 |
5 files changed, 583 insertions, 2 deletions
diff --git a/flatbuffers/patio.fbs b/flatbuffers/patio.fbs new file mode 100644 index 00000000..7ffcb051 --- /dev/null +++ b/flatbuffers/patio.fbs @@ -0,0 +1,484 @@ +include "sources.fbs"; + +namespace Patio; + +// == Data layouts == + +union PatioDataLayout { + PatioDataLayoutArray, + PatioDataLayoutBool, + PatioDataLayoutByte, + PatioDataLayoutDataPtr, + PatioDataLayoutFunctionPtr, + PatioDataLayoutInt, + PatioDataLayoutInt32, + PatioDataLayoutStruct, +} + +table PatioDataLayoutArray { + element:PatioDataLayout; + size:uint; +} + +table PatioDataLayoutBool { +} + +table PatioDataLayoutByte { +} + +table PatioDataLayoutDataPtr { +} + +table PatioDataLayoutFunctionPtr { +} + +table PatioDataLayoutInt { +} + +table PatioDataLayoutInt32 { +} + +table PatioDataLayoutStruct { + elements:[PatioDataLayout]; +} + +// == Data ptr values == + +union PatioDataPtrValue { + PatioDataPtrValueDataElement, + PatioDataPtrValueNull, +} + +table PatioDataPtrValueDataElement { + name:string; +} + +table PatioDataPtrValueNull { +} + +// == Int values == + +union PatioIntValue { + PatioIntValueByteOffsetOf, + PatioIntValueByteSizeOf, + PatioIntValueLiteral, +} + +table PatioIntValueByteOffsetOf { + layout:PatioDataLayout; + offset:[uint]; +} + +table PatioIntValueByteSizeOf { + layout:PatioDataLayout; +} + +table PatioIntValueLiteral { + value:int; +} + +// == Data values == + +union PatioDataValue { + PatioDataValueArray, + PatioDataValueBool, + PatioDataValueByte, + PatioDataValueDataPtr, + PatioDataValueInt, + PatioDataValueInt32, + PatioDataValueStruct, +} + +table PatioDataValueArray { + element_layout:PatioDataLayout; + elements:[PatioDataValue]; +} + +table PatioDataValueBool { + value:bool; +} + +table PatioDataValueByte { + value:uint; +} + +table PatioDataValueDataPtr { + value:PatioDataPtrValue; +} + +table PatioDataValueInt { + value:PatioIntValue; +} + +table PatioDataValueInt32 { + value:PatioIntValue; +} + +table PatioDataValueStruct { + elements:[PatioDataValue]; +} + +// == Types == + +union PatioType { + PatioBoolType, + PatioByteType, + PatioDataPtrType, + PatioFunctionPtrType, + PatioIntType, + PatioInt32Type, + PatioVoidType, +} + +table PatioBoolType { +} + +table PatioByteType { +} + +table PatioDataPtrType { +} + +table PatioFunctionPtrType { +} + +table PatioIntType { +} + +table PatioInt32Type { +} + +table PatioVoidType { +} + +// == Operands == + +union PatioOperand { + PatioBoolConst, + PatioByteConst, + PatioDataPtrConst, + PatioFunctionPtrConst, + PatioIntConst, + PatioInt32Const, + PatioLocalRef, +} + +table PatioBoolConst { + value:bool; +} + +table PatioByteConst { + value:uint; +} + +table PatioDataPtrConst { + value:PatioDataPtrValue; +} + +table PatioFunctionPtrConst { + name:string; +} + +table PatioIntConst { + value:PatioIntValue; +} + +table PatioInt32Const { + value:PatioIntValue; +} + +table PatioLocalRef { + name:string; +} + +// == Instructions == + +union PatioInstruction { + PatioBlock, + PatioBoolToInt, + PatioCall, + PatioCallPointer, + PatioDataPtrToInt, + PatioIf, + PatioIntAdd, + PatioIntEquals, + PatioIntNotEqual, + PatioIntSub, + PatioIntToBool, + PatioIntToDataPtr, + PatioLocalStore, + PatioLoop, + PatioLoopBreak, + PatioLoopContinue, + PatioMemoryLoad, + PatioMemoryStore, + PatioResume, + PatioReturn, + PatioReturnVoid, + PatioSwitch, + PatioThrow, + PatioTry, + PatioUnreachable, + PatioVarLoad, + PatioVarStore, +} + +table PatioBlock { + source:Sources.Source; + body:[PatioInstruction]; + results:[PatioOperand]; +} + +table PatioBoolToInt { + source:Sources.Source; + destination:PatioLocalRef; + value:PatioOperand; +} + +table PatioCall { + source:Sources.Source; + destination:PatioLocalRef; + function_name:string; + args:[PatioOperand]; +} + +table PatioCallPointer { + source:Sources.Source; + destination:PatioLocalRef; + function_pointer:PatioOperand; + args:[PatioOperand]; +} + +table PatioDataPtrToInt { + source:Sources.Source; + destination:PatioLocalRef; + value:PatioOperand; +} + +table PatioIf { + source:Sources.Source; + destinations:[PatioLocalRef]; + condition:PatioOperand; + true_body:PatioBlock; + false_body:PatioBlock; +} + +table PatioIntAdd { + source:Sources.Source; + destination:PatioLocalRef; + left:PatioOperand; + right:PatioOperand; +} + +table PatioIntEquals { + source:Sources.Source; + destination:PatioLocalRef; + left:PatioOperand; + right:PatioOperand; +} + +table PatioIntNotEqual { + source:Sources.Source; + destination:PatioLocalRef; + left:PatioOperand; + right:PatioOperand; +} + +table PatioIntSub { + source:Sources.Source; + destination:PatioLocalRef; + left:PatioOperand; + right:PatioOperand; +} + +table PatioIntToBool { + source:Sources.Source; + destination:PatioLocalRef; + value:PatioOperand; +} + +table PatioIntToDataPtr { + source:Sources.Source; + destination:PatioLocalRef; + value:PatioOperand; +} + +table PatioLocalStore { + source:Sources.Source; + destination:PatioLocalRef; + value:PatioOperand; +} + +table PatioLoop { + source:Sources.Source; + body:[PatioInstruction]; +} + +table PatioLoopBreak { + source:Sources.Source; +} + +table PatioLoopContinue { + source:Sources.Source; +} + +table PatioMemoryLoad { + source:Sources.Source; + destination:PatioLocalRef; + pointer:PatioOperand; + layout:PatioDataLayout; + offset:[uint]; +} + +table PatioMemoryStore { + source:Sources.Source; + pointer:PatioOperand; + layout:PatioDataLayout; + offset:[uint]; + value:PatioOperand; +} + +table PatioResume { + source:Sources.Source; + value:PatioOperand; +} + +table PatioReturn { + source:Sources.Source; + value:PatioOperand; +} + +table PatioReturnVoid { + source:Sources.Source; +} + +table PatioSwitch { + source:Sources.Source; + destinations:[PatioLocalRef]; + value:PatioOperand; + cases:[PatioSwitchCase]; + default_body:PatioBlock; +} + +table PatioSwitchCase { + source:Sources.Source; + value:uint; + body:PatioBlock; +} + +table PatioThrow { + source:Sources.Source; + exception_type_name:string; + value:PatioOperand; +} + +table PatioTry { + source:Sources.Source; + destinations:[PatioLocalRef]; + catch_clauses:[PatioCatchClause]; + body:PatioBlock; +} + +table PatioCatchClause { + source:Sources.Source; + exception_type_name:string; + destination:PatioLocalRef; + body:PatioBlock; +} + +table PatioUnreachable { + source:Sources.Source; +} + +table PatioVarLoad { + source:Sources.Source; + destination:PatioLocalRef; + var:PatioLocalRef; +} + +table PatioVarStore { + source:Sources.Source; + var:PatioLocalRef; + value:PatioOperand; +} + +// == Visibility == + +enum PatioVisibility : int { + PRIVATE = 0, + PUBLIC = 1, +} + +// == Locals == + +enum PatioLocalType : int { + PARAM = 0, + VAL = 1, + VAR = 2, +} + +table PatioLocal { + name:string; + type:PatioType; + local_type:PatioLocalType; +} + +// == Compilation unit elements == + +union PatioCompilationUnitElement { + PatioDataElementImmutableDefinition, + PatioDataElementMutableDeclaration, + PatioDataElementMutableDefinition, + PatioExceptionTypeDefinition, + PatioFunctionDeclaration, + PatioFunctionDefinition, +} + +table PatioDataElementImmutableDefinition { + source:Sources.Source; + name:string; + visibility:PatioVisibility; + value:PatioDataValue; +} + +table PatioDataElementMutableDeclaration { + source:Sources.Source; + name:string; + layout:PatioDataLayout; +} + +table PatioDataElementMutableDefinition { + source:Sources.Source; + name:string; + visibility:PatioVisibility; + initial_value:PatioDataValue; +} + +table PatioExceptionTypeDefinition { + source:Sources.Source; + name:string; + value_type:PatioType; +} + +table PatioFunctionDeclaration { + source:Sources.Source; + name:string; + params:[PatioLocal]; + return_type:PatioType; +} + +table PatioFunctionDefinition { + source:Sources.Source; + name:string; + visibility:PatioVisibility; + locals:[PatioLocal]; + return_type:PatioType; + body:[PatioInstruction]; +} + +// == Compilation unit == + +table PatioCompilationUnit { + source:Sources.Source; + elements:[PatioCompilationUnitElement]; +} diff --git a/flatbuffers/sources.fbs b/flatbuffers/sources.fbs new file mode 100644 index 00000000..b39d65f5 --- /dev/null +++ b/flatbuffers/sources.fbs @@ -0,0 +1,25 @@ +namespace Sources; + +union Source { + NullSource, + RuntimeSource, + FileFragmentSource, +} + +table NullSource { +} + +table RuntimeSource { +} + +table FileFragmentSource { + source_text:SourceText; + start_character_index:uint; + end_character_index:uint; +} + +table SourceText { + name:string; + contents:string; +} + diff --git a/generate-flatbuffers.py b/generate-flatbuffers.py new file mode 100644 index 00000000..986e6e8f --- /dev/null +++ b/generate-flatbuffers.py @@ -0,0 +1,56 @@ +import os +import pathlib +import re +import shutil +import subprocess +import tempfile + + +def _main(): + os.chdir(os.path.dirname(__file__)) + + with tempfile.TemporaryDirectory() as temp_dir: + temp_dir = pathlib.Path(temp_dir) + schema_filenames = ["sources.fbs", "patio.fbs"] + namespace_map = {} + type_map = {} + + for schema_filename in schema_filenames: + with open(pathlib.Path("flatbuffers") / schema_filename, "rt") as source_schema_file: + source_schema = list(source_schema_file) + + for line_index, line in enumerate(source_schema): + namespace_match = re.match("^namespace (.*);$", line) + if namespace_match is not None: + source_namespace = namespace_match[1] + dest_namespace = f"org.shedlang.compiler.{source_namespace.lower()}.flatbuffers" + namespace_map[source_namespace] = dest_namespace + source_schema[line_index] = f"namespace {dest_namespace};" + + type_match = re.match(r"^((?:enum|table|union)\s+)(\S+)(\s+.*{)", line) + if type_match is not None: + source_type = type_match[2] + dest_type = f"{source_type}Flatbuffers" + type_map[source_type] = dest_type + source_schema[line_index] = f"{type_match[1]}{dest_type}{type_match[3]}" + + with open(temp_dir / schema_filename, "wt") as dest_schema_file: + for line in source_schema: + for source_namespace, dest_namespace in namespace_map.items(): + line = line.replace(f":{source_namespace}.", f":{dest_namespace}.") + for source_type, dest_type in type_map.items(): + line = re.sub(rf"\b{source_type}\b", dest_type, line) + + dest_schema_file.write(line) + + gen_path = pathlib.Path("src/gen/java") + subprocess.run( + ["flatc", "-o", gen_path, "--java"] + [ + temp_dir / schema_filename + for schema_filename in schema_filenames + ], + check=True, + ) + + +_main() @@ -23,9 +23,20 @@ runtime: native-headers build-runtime build-runtime: python3 runtime/build.py standard -.PHONY: protobuf +.PHONY: src/gen/java -protobuf: +src/gen/java: rm -rf src/gen/java mkdir -p src/gen/java + make flatbuffers + make protobuf + +.PHONY: protobuf + +protobuf: protoc -I protobuf --java_out src/gen/java protobuf/*.proto + +.PHONY: flatbuffers + +flatbuffers: + python3 generate-flatbuffers.py @@ -74,6 +74,11 @@ <scope>test</scope> </dependency> <dependency> + <groupId>com.google.flatbuffers</groupId> + <artifactId>flatbuffers-java</artifactId> + <version>25.2.10</version> + </dependency> + <dependency> <groupId>com.google.protobuf</groupId> <artifactId>protobuf-java</artifactId> <version>4.34.1</version> |
