FormCompiler: Start working on a C++ code generator for VisualBuilder forms.

This commit is contained in:
Andreas Kling 2019-07-10 20:41:31 +02:00
parent 69fea8d41d
commit 2d3293dfbd
Notes: sideshowbarker 2024-07-19 13:20:52 +09:00
3 changed files with 77 additions and 0 deletions

View File

@ -0,0 +1,28 @@
PROGRAM = FormCompiler
SOURCES = \
main.cpp \
../../AK/String.cpp \
../../AK/StringImpl.cpp \
../../AK/StringBuilder.cpp \
../../AK/StringView.cpp \
../../AK/JsonObject.cpp \
../../AK/JsonValue.cpp \
../../AK/JsonArray.cpp \
../../AK/JsonParser.cpp \
../../AK/LogStream.cpp \
../../Libraries/LibCore/CIODevice.cpp \
../../Libraries/LibCore/CFile.cpp \
../../Libraries/LibCore/CObject.cpp \
../../Libraries/LibCore/CEvent.cpp \
../../Libraries/LibCore/CEventLoop.cpp
all: $(PROGRAM)
CXXFLAGS = -std=c++17 -Wall -Wextra
$(PROGRAM): $(SOURCES)
$(CXX) $(CXXFLAGS) -I../ -I../../ -I../../Libraries/ -o $@ $(SOURCES)
clean:
rm -f $(PROGRAM)

View File

@ -0,0 +1 @@
{"name":"Form1","widgets":[{"name":"w1","tooltip":null,"backcolor":"#d4d0c8ff","forecolor":"#000000ff","text":"Hello world!","class":"GLabel","autofill":true,"enabled":true,"visible":true,"x":5,"height":26,"y":5,"width":121},{"name":"w2","tooltip":null,"backcolor":"#d4d0c8ff","forecolor":"#000000ff","text":"Lefty","class":"GButton","autofill":false,"enabled":true,"visible":true,"x":5,"height":26,"y":40,"width":51},{"name":"w3","tooltip":null,"backcolor":"#d4d0c8ff","forecolor":"#000000ff","text":"Righty","class":"GButton","autofill":false,"enabled":true,"visible":true,"x":80,"height":26,"y":40,"width":51},{"name":"w4","enabled":true,"forecolor":"#000000ff","checked":false,"autofill":false,"x":150,"tooltip":null,"height":26,"width":91,"y":5,"class":"GCheckBox","text":"Awesome","backcolor":"#d4d0c8ff","visible":true},{"name":"w5","enabled":true,"forecolor":"#000000ff","checked":false,"autofill":false,"x":150,"tooltip":null,"height":26,"width":51,"y":30,"class":"GCheckBox","text":"Cool","backcolor":"#d4d0c8ff","visible":true}]}

View File

@ -0,0 +1,48 @@
#include <AK/JsonArray.h>
#include <AK/JsonObject.h>
#include <AK/JsonValue.h>
#include <AK/LogStream.h>
#include <LibCore/CFile.h>
#include <stdio.h>
int main(int argc, char** argv)
{
if (argc != 2) {
printf("usage: %s <form-file>\n", argv[0]);
return 0;
}
CFile file(argv[1]);
if (!file.open(CIODevice::ReadOnly)) {
fprintf(stderr, "Error: Cannot open %s: %s\n", argv[1], file.error_string());
return 1;
}
auto file_contents = file.read_all();
auto json = JsonValue::from_string(file_contents);
if (!json.is_object()) {
fprintf(stderr, "Malformed input\n");
return 1;
}
auto name = json.as_object().get("name").to_string();
auto widgets = json.as_object().get("widgets");
if (!widgets.is_array()) {
fprintf(stderr, "Malformed input\n");
return 1;
}
dbg() << "auto* main_widget = new GWidget(nullptr);";
widgets.as_array().for_each([&](auto& value) {
ASSERT(value.is_object());
const JsonObject& widget_object = value.as_object();
auto name = widget_object.get("name").to_string();
auto class_name = widget_object.get("class").to_string();
dbg() << "auto* " << name << " = new " << class_name << "(main_widget);";
});
return 0;
}