Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions books/crafting-interpreters/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/ltximg/
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
helloworld
helloworld.exe
*~
*.o
20 changes: 20 additions & 0 deletions books/crafting-interpreters/01-introduction/helloworld-c/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
SRCS := $(wildcard *.c)
OBJS := $(patsubst %.c,%.o,$(SRCS))
CC := gcc
CFLAGS := -Wall -g
OBJFLAG := -c
OUTFLAG := -o

.PHONY: all clean

all: helloworld

helloworld: $(OBJS)
$(CC) $(CFLAGS) $(OUTFLAG) $@ $^

%.o: %.c
$(CC) $(CFLAGS) $(OUTFLAG) $@ $(OBJFLAG) $^

clean:
rm *.o

48 changes: 48 additions & 0 deletions books/crafting-interpreters/01-introduction/helloworld-c/list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "list.h"
#include <stdlib.h>
#include <string.h>

void
list_insert(list_node_t **list, char *string)
{
list_node_t *end = *list;
if(end == NULL) {
*list = end = malloc(sizeof(list_node_t));
end->prev = end->next = NULL;
} else {
while(end->next != NULL)
end = end->next;
end->next = malloc(sizeof(list_node_t));
end->next->prev = end;
end->next->next = NULL;
end = end->next;
}

end->string = malloc(strlen(string) * sizeof(char));
strcpy(end->string, string);
}

list_node_t *
list_find(list_node_t *list, const char *string)
{
if(list == NULL) return NULL;
list_node_t *itr = list;
do {
if(strcmp(string, itr->string) == 0) break;
itr = itr->next;
} while(itr != NULL);
return itr;
}

void
list_delete(list_node_t **node)
{
if(*node == NULL) return;

list_node_t *aux = *node;
if(aux->prev != NULL) aux->prev->next = aux->next;
if(aux->next != NULL) aux->next->prev = aux->prev;
free(aux->string);
*node = aux->next;
free(aux);
}
14 changes: 14 additions & 0 deletions books/crafting-interpreters/01-introduction/helloworld-c/list.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef LIST_H
#define LIST_H

typedef struct LIST_NODE_T {
char *string;
struct LIST_NODE_T *prev;
struct LIST_NODE_T *next;
} list_node_t;

void list_insert(list_node_t **list, char *string);
list_node_t *list_find(list_node_t *list, const char *string);
void list_delete(list_node_t **node);

#endif
50 changes: 50 additions & 0 deletions books/crafting-interpreters/01-introduction/helloworld-c/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <stdio.h>
#include "list.h"

int
main(void)
{
printf("Hello, world!\n");

list_node_t *list = NULL;
list_insert(&list, "Hello");
list_insert(&list, "Cruel");
list_insert(&list, "World");

printf("List contents:\n");
for(const list_node_t *itr = list; itr != NULL; itr = itr->next) {
printf(" - %s\n", itr->string);
}
putchar('\n');

printf("Find word 'Cruel': ");
list_node_t *match = list_find(list, "Cruel");
printf("%s\n", (match != NULL) ? "Found" : "Not found");

printf("Remove word 'Cruel'... ");
list_delete(&match);
printf("Done\n");

printf("Find word 'World'... ");
match = list_find(list, "World");
printf("%s\n", (match != NULL) ? "Found" : "Not found");

printf("List contents backwards:\n");
for(const list_node_t *itr = match; itr != NULL; itr = itr->prev) {
printf(" - %s\n", itr->string);
}
putchar('\n');

printf("Clear list... ");
int i = 0;
while(list != NULL) {
list_delete(&list);
printf("%d... ", ++i);
}
printf("Done\n");

printf("Check if list was cleared... %s\n",
(list == NULL) ? "Yes" : "No");

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>helloworld</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>24</maven.compiler.source>
<maven.compiler.target>24</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.example;

public class Main {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
Loading