add gtk3
This commit is contained in:
parent
e6edf7bb42
commit
32964b393b
4
Makefile
4
Makefile
@ -6,8 +6,8 @@ TARGET = matemateak
|
||||
CFLAGS = -std=c17 -Wall -Wextra -Wpedantic -Wconversion
|
||||
LFLAGS = -lm
|
||||
|
||||
CFLAGS += $(shell pkg-config --cflags gtk+-2.0)
|
||||
LFLAGS += $(shell pkg-config --libs gtk+-2.0)
|
||||
CFLAGS += $(shell pkg-config --cflags gtk+-3.0)
|
||||
LFLAGS += $(shell pkg-config --libs gtk+-3.0)
|
||||
|
||||
C_FILES = $(shell find src/ -name *.c)
|
||||
HEADER_FILES = $(shell find src/ -name *.h)
|
||||
|
87
src/main.c
87
src/main.c
@ -1,75 +1,38 @@
|
||||
#include "lexer.h"
|
||||
#include "parser.h"
|
||||
#include <gtk/gtk.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
void test_lexer_parser(void)
|
||||
static void button_handler(GtkWidget* widget, gpointer user_data)
|
||||
{
|
||||
char* text = "2 * (3 + 4)";
|
||||
g_print("Button pressed!\n");
|
||||
}
|
||||
|
||||
Lexer lexer1;
|
||||
lexer(&lexer1, text, strlen(text));
|
||||
Token current_token = lexer_next(&lexer1);
|
||||
printf("tokens = [\n");
|
||||
while (current_token.type != TokenTypeEof) {
|
||||
char* token_string = token_to_string(¤t_token, text);
|
||||
printf(" %s,\n", token_string);
|
||||
free(token_string);
|
||||
current_token = lexer_next(&lexer1);
|
||||
}
|
||||
printf("]\n");
|
||||
static void activate(GtkApplication* app, gpointer user_data)
|
||||
{
|
||||
GtkWidget* window = gtk_application_window_new(app);
|
||||
gtk_window_set_title(GTK_WINDOW(window), "MatemaTeak");
|
||||
gtk_window_set_default_size(GTK_WINDOW(window), 1600, 900);
|
||||
|
||||
Lexer lexer2;
|
||||
lexer(&lexer2, text, strlen(text));
|
||||
Expr* ast = parse(&lexer2, text, strlen(text));
|
||||
char* ast_string = expr_to_string(ast);
|
||||
printf("ast = %s\n", ast_string);
|
||||
free(ast_string);
|
||||
free_expr(ast);
|
||||
GtkWidget* button_box = gtk_button_box_new(GTK_ORIENTATION_HORIZONTAL);
|
||||
gtk_container_add(GTK_CONTAINER(window), button_box);
|
||||
|
||||
GtkWidget* button = gtk_button_new_with_label("Do stuff");
|
||||
g_signal_connect(button, "clicked", G_CALLBACK(button_handler), NULL);
|
||||
g_signal_connect_swapped(
|
||||
button, "clicked", G_CALLBACK(gtk_widget_destroy), window
|
||||
);
|
||||
gtk_container_add(GTK_CONTAINER(button_box), button);
|
||||
|
||||
gtk_widget_show_all(window);
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
GtkWidget* window;
|
||||
GtkWidget* vbox;
|
||||
|
||||
GtkWidget* menubar;
|
||||
GtkWidget* fileMenu;
|
||||
GtkWidget* fileMi;
|
||||
GtkWidget* quitMi;
|
||||
|
||||
gtk_init(&argc, &argv);
|
||||
|
||||
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
|
||||
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
|
||||
gtk_window_set_default_size(GTK_WINDOW(window), 300, 200);
|
||||
gtk_window_set_title(GTK_WINDOW(window), "Simple menu");
|
||||
|
||||
vbox = gtk_vbox_new(FALSE, 0);
|
||||
gtk_container_add(GTK_CONTAINER(window), vbox);
|
||||
|
||||
menubar = gtk_menu_bar_new();
|
||||
fileMenu = gtk_menu_new();
|
||||
|
||||
fileMi = gtk_menu_item_new_with_label("File");
|
||||
quitMi = gtk_menu_item_new_with_label("Quit");
|
||||
|
||||
gtk_menu_item_set_submenu(GTK_MENU_ITEM(fileMi), fileMenu);
|
||||
gtk_menu_shell_append(GTK_MENU_SHELL(fileMenu), quitMi);
|
||||
gtk_menu_shell_append(GTK_MENU_SHELL(menubar), fileMi);
|
||||
gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0);
|
||||
|
||||
g_signal_connect(
|
||||
G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL
|
||||
GtkApplication* app = gtk_application_new(
|
||||
"dk.simonfj20.matemateak", G_APPLICATION_DEFAULT_FLAGS
|
||||
);
|
||||
|
||||
g_signal_connect(
|
||||
G_OBJECT(quitMi), "activate", G_CALLBACK(gtk_main_quit), NULL
|
||||
);
|
||||
|
||||
gtk_widget_show_all(window);
|
||||
|
||||
gtk_main();
|
||||
g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
|
||||
int status = g_application_run(G_APPLICATION(app), argc, argv);
|
||||
g_object_unref(app);
|
||||
return status;
|
||||
}
|
||||
|
30
src/parser_test.c
Normal file
30
src/parser_test.c
Normal file
@ -0,0 +1,30 @@
|
||||
#include "lexer.h"
|
||||
#include "parser.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
void test_lexer_parser(void)
|
||||
{
|
||||
char* text = "2 * (3 + 4)";
|
||||
|
||||
Lexer lexer1;
|
||||
lexer(&lexer1, text, strlen(text));
|
||||
Token current_token = lexer_next(&lexer1);
|
||||
printf("tokens = [\n");
|
||||
while (current_token.type != TokenTypeEof) {
|
||||
char* token_string = token_to_string(¤t_token, text);
|
||||
printf(" %s,\n", token_string);
|
||||
free(token_string);
|
||||
current_token = lexer_next(&lexer1);
|
||||
}
|
||||
printf("]\n");
|
||||
|
||||
Lexer lexer2;
|
||||
lexer(&lexer2, text, strlen(text));
|
||||
Expr* ast = parse(&lexer2, text, strlen(text));
|
||||
char* ast_string = expr_to_string(ast);
|
||||
printf("ast = %s\n", ast_string);
|
||||
free(ast_string);
|
||||
free_expr(ast);
|
||||
}
|
Loading…
Reference in New Issue
Block a user