From 1b0003e586517806b7f170b10a3b96735dde2435 Mon Sep 17 00:00:00 2001 From: ctsk <9384305+ctsk@users.noreply.github.com> Date: Fri, 2 Sep 2022 21:57:44 +0200 Subject: [PATCH] Challenge 1 + 2 --- .gitignore | 3 ++ challenges/01-intro-challenges/answers.md | 9 ++++ challenges/01-intro-challenges/c-env/Makefile | 10 +++++ .../01-intro-challenges/c-env/linked_list.c | 44 +++++++++++++++++++ .../01-intro-challenges/c-env/linked_list.h | 13 ++++++ challenges/01-intro-challenges/c-env/main.c | 18 ++++++++ .../java-env/HelloWorld.java | 5 +++ .../01-intro-challenges/java-env/Makefile | 7 +++ .../02-map-territory-challenges/answers.md | 15 +++++++ 9 files changed, 124 insertions(+) create mode 100644 .gitignore create mode 100644 challenges/01-intro-challenges/answers.md create mode 100644 challenges/01-intro-challenges/c-env/Makefile create mode 100644 challenges/01-intro-challenges/c-env/linked_list.c create mode 100644 challenges/01-intro-challenges/c-env/linked_list.h create mode 100644 challenges/01-intro-challenges/c-env/main.c create mode 100644 challenges/01-intro-challenges/java-env/HelloWorld.java create mode 100644 challenges/01-intro-challenges/java-env/Makefile create mode 100644 challenges/02-map-territory-challenges/answers.md diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ad3d1c5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.o +*.out +*.class diff --git a/challenges/01-intro-challenges/answers.md b/challenges/01-intro-challenges/answers.md new file mode 100644 index 0000000..e23c2bd --- /dev/null +++ b/challenges/01-intro-challenges/answers.md @@ -0,0 +1,9 @@ +DSLs found in [https://github.com/munificent/craftinginterpreters] +---- + +1. HTML +2. Jinja2 +2. CSS +3. SASS +4. Markdown +7. Makefile diff --git a/challenges/01-intro-challenges/c-env/Makefile b/challenges/01-intro-challenges/c-env/Makefile new file mode 100644 index 0000000..9e6684a --- /dev/null +++ b/challenges/01-intro-challenges/c-env/Makefile @@ -0,0 +1,10 @@ +run: main + @ ./main + +main: main.c linked_list.o + @ gcc $^ -o main + +linked_list.o: linked_list.c + @ gcc -c linked_list.c + + diff --git a/challenges/01-intro-challenges/c-env/linked_list.c b/challenges/01-intro-challenges/c-env/linked_list.c new file mode 100644 index 0000000..0bcb9f3 --- /dev/null +++ b/challenges/01-intro-challenges/c-env/linked_list.c @@ -0,0 +1,44 @@ +#include "malloc.h" +#include "linked_list.h" + +List* InitList() { + List *new = (List *) malloc(sizeof(List)); + new->head = NULL; + return new; +} + +int Insert(List *l, char *data) { + Item *new = (Item *) malloc(sizeof(Item)); + node->data = malloc(strlen(string) + 1); + strcpy(node->data, string); + + if (l->head = NULL) { + new->next = new; + new->prev = new; + l->head = new; + return; + } else { + new->next = l->head; + new->prev = l->head->prev; + l->head->prev->next = new; + l->head->prev = new; + l->head = new; + } +} + +Item* Find(List *l, char *data) { + if (l->head == NULL) { + return NULL; + } + + Item *cur = l->head; + do { + if (strcmp(data, cur->data) == 0) { + return cur; + } + + cur = cur->next; + } while (cur != l->head); + + return NULL; +} diff --git a/challenges/01-intro-challenges/c-env/linked_list.h b/challenges/01-intro-challenges/c-env/linked_list.h new file mode 100644 index 0000000..c56dbba --- /dev/null +++ b/challenges/01-intro-challenges/c-env/linked_list.h @@ -0,0 +1,13 @@ +typedef struct _Item { + char *data; + struct _Item *prev; + struct _Item *next; +} Item; + +typedef struct List { + Item *head; +} List; + +List* InitList(); +void Insert(List *l, char *data); +Item* Find(List *l, char *data); diff --git a/challenges/01-intro-challenges/c-env/main.c b/challenges/01-intro-challenges/c-env/main.c new file mode 100644 index 0000000..a6d6a70 --- /dev/null +++ b/challenges/01-intro-challenges/c-env/main.c @@ -0,0 +1,18 @@ +#include +#include "linked_list.h" +#include + +int main() +{ + List *l = InitList(); + + Insert(l, "one"); + Insert(l, "two"); + Insert(l, "three"); + + int f = Find(l, "two"); + assert(f > 0); + + int f = Find(l, "four"); + assert(f == 0); +} diff --git a/challenges/01-intro-challenges/java-env/HelloWorld.java b/challenges/01-intro-challenges/java-env/HelloWorld.java new file mode 100644 index 0000000..167c4ab --- /dev/null +++ b/challenges/01-intro-challenges/java-env/HelloWorld.java @@ -0,0 +1,5 @@ +public class HelloWorld { + public static void main(String[] args) { + System.out.println("Hello World"); + } +} diff --git a/challenges/01-intro-challenges/java-env/Makefile b/challenges/01-intro-challenges/java-env/Makefile new file mode 100644 index 0000000..647a4f2 --- /dev/null +++ b/challenges/01-intro-challenges/java-env/Makefile @@ -0,0 +1,7 @@ +run: HelloWorld.class + @ java HelloWorld + +HelloWorld.class: HelloWorld.java + @ javac $^ + +.PHONY: run diff --git a/challenges/02-map-territory-challenges/answers.md b/challenges/02-map-territory-challenges/answers.md new file mode 100644 index 0000000..c31d389 --- /dev/null +++ b/challenges/02-map-territory-challenges/answers.md @@ -0,0 +1,15 @@ +1. Java (OpenJDK) + -> No Flex/Yacc used + -> Files: + Scanner: [jdk/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/Scanner.java] + Parser: [jdk/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java] + [https://github.com/openjdk/jdk/blob/1f484dae4efaa60cf18a3d4df947c05f1497bd5b] + +2. Reasons not to JIT + - Short running programs => Cost of compilation higher than gained speed + - Little repeat execution of code + - Platform-independence + +3. Why do Lisps contain interpreters? + - Better interactivity + - Evaluation of Macros