Skip to content

Commit

Permalink
Merge pull request #72 from ldilley/master
Browse files Browse the repository at this point in the history
Docker/Podman support, interpret() prototype correction, and musl printf flush fix
  • Loading branch information
ldilley authored Sep 25, 2023
2 parents 4318065 + bfaed5a commit 33b93cf
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 53 deletions.
55 changes: 33 additions & 22 deletions .github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,44 +18,44 @@ For more information about Concoct, please see the [wiki](https://github.com/Con

#### Linux/Unix and Cygwin Steps
1. Install prerequisites:

Debian/Ubuntu:
```sh
# apt-get install cmake gcc git make
```
apt-get install cmake gcc git make
```
CentOS/Fedora/RHEL:
```sh
# yum install cmake gcc git make
```
yum install cmake gcc git make
```
FreeBSD:
```sh
# pkg install clang cmake git make
```
1. Obtain the source code via `git` or download a zip archive:
```sh
$ git clone https://github.com/Concoctist/concoct.git
pkg install clang cmake git make
```

2. Obtain the source code via `git` or download a zip archive:
```
git clone https://github.com/Concoctist/concoct.git
```
Or:
```sh
$ wget https://github.com/Concoctist/concoct/archive/master.zip && unzip master.zip
```
wget https://github.com/Concoctist/concoct/archive/master.zip && unzip master.zip
```

2. In the top-level directory where `CMakeLists.txt` exists, create a build directory:
```sh
$ mkdir bld && cd bld
3. In the top-level directory where `CMakeLists.txt` exists, create a build directory:
```
mkdir bld && cd bld
```

3. Generate the `Makefile` (you can alternatively use `ccmake` here if you prefer):
```sh
$ cmake ..
4. Generate the `Makefile` (you can alternatively use `ccmake` here if you prefer):
```
cmake ..
```

4. Build Concoct:
```sh
$ make
5. Build Concoct:
```
make
```

5. There should now be a `concoct` executable under the `bin` directory if the build was successful:
6. There should now be a `concoct` executable under the `bin` directory if the build was successful:
```
$ ./concoct -v
Concoct v0.1.0 rev 148 (d976be2) (64-bit Linux) (Debug) built at 00:46 on 01-02-2022
Expand Down Expand Up @@ -89,6 +89,17 @@ For more information about Concoct, please see the [wiki](https://github.com/Con

![image](https://user-images.githubusercontent.com/3323717/147864334-5c8d44f3-136f-47f9-a1d5-434826e6572d.png)

#### Docker/Podman Steps
1. Build image:
```
podman build -t concoct -f Containerfile
```

2. Run container:
```
podman run -it localhost/concoct
```

### Contributing and Support :octocat:
Feel free to [submit an issue](https://github.com/Concoctist/concoct/issues/new) if you require assistance or would like to
make a feature request. You are also welcome to join our Discord server at https://discord.concoct.ist/. Any contributions such as build testing, creating bug reports
Expand Down
15 changes: 15 additions & 0 deletions Containerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Concoct - An imperative, dynamically-typed, interpreted, general-purpose programming language
# Copyright (c) 2020-2023 BlakeTheBlock and Lloyd Dilley
# http://concoct.ist/

FROM alpine:latest AS construct
RUN apk update && apk add clang cmake git make musl-dev && apk cache clean
RUN cd /root && git clone https://github.com/concoctist/concoct.git
RUN cd /root/concoct && mkdir bld && cd bld && cmake .. && make

FROM alpine:latest
RUN adduser -h /home/concoct -g "Concoct Account" -S concoct
USER concoct
WORKDIR /home/concoct
COPY --from=construct --chown=concoct /root/concoct/bld/bin/concoct .
CMD ["/home/concoct/concoct"]
6 changes: 5 additions & 1 deletion include/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@
#ifndef COMPILER_H
#define COMPILER_H

#include <stdbool.h> // bool
#include <stdbool.h> // bool
#include "hash_map.h"
#include "parser.h"
#include "vm/opcodes.h"

#define MAX_QUEUE_CAPACITY ((size_t)256)

Expand Down Expand Up @@ -66,6 +67,9 @@ void dequeue(Queue* queue, ConcoctNode** node);
// Inserts new node into queue
void enqueue(Queue* queue, ConcoctNode* node);

// Swap last 2 stack objects to fix order if first instruction is a binary operation
void swap_last_operands(Opcode oc);

// Translates lexer/parser tokens to VM instructions
void compile(ConcoctNodeTree* tree, ConcoctHashMap* map);

Expand Down
10 changes: 9 additions & 1 deletion include/vm/opcodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#ifndef OPCODES_H
#define OPCODES_H

#include <stdbool.h> // bool

// Supported instruction set
typedef enum opcode
{
Expand Down Expand Up @@ -66,7 +68,7 @@ typedef enum opcode
OP_MOD, // modulo (%)
OP_MOV, // move (from register to register)
OP_MUL, // multiply (*)
OP_NEG, // negative
OP_NEG, // negative (unary -)
OP_NEQ, // not equal to (!=)
OP_NOP, // no op
OP_NOT, // logical not/negation (!)
Expand All @@ -93,4 +95,10 @@ typedef enum opcode
// Returns opcode constant based on numeric ID
const char* get_mnemonic(Opcode oc);

// Returns true if opcode is a unary operation
bool is_unary_operation(Opcode oc);

// Returns true if opcode is a binary operation
bool is_binary_operation(Opcode oc);

#endif // OPCODES_H
3 changes: 2 additions & 1 deletion include/vm/vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#ifndef VM_H
#define VM_H

#include "hash_map.h"
#include "stack.h"
#include "types.h" // BigNum, Byte
#include "vm/opcodes.h" // Opcode
Expand Down Expand Up @@ -91,6 +92,6 @@ void init_vm();
void stop_vm();

// Interprets code
RunCode interpret();
RunCode interpret(ConcoctHashMap* map);

#endif // VM_H
26 changes: 23 additions & 3 deletions src/compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,22 @@ void enqueue(Queue* queue, ConcoctNode* node)
return;
}

// Swap last 2 stack objects to fix order if first instruction is a binary operation
void swap_last_operands(Opcode oc)
{
Object* object1 = NULL;
Object* object2 = NULL;
if(!is_binary_operation(oc))
return;
if(debug_mode)
debug_print("Swapping top 2 objects of stack...");
object1 = pop(vm.sp);
object2 = pop(vm.sp);
push(vm.sp, object1);
push(vm.sp, object2);
return;
}

// Translates lexer/parser tokens to VM instructions
void compile(ConcoctNodeTree* tree, ConcoctHashMap* map)
{
Expand Down Expand Up @@ -288,9 +304,13 @@ void compile(ConcoctNodeTree* tree, ConcoctHashMap* map)
enqueue(pqueue, current->children[i]);
}

reverse_instructions(ic);
vm.instructions[ic] = OP_END; // append end instruction
interpret(map);
if(ic > 0)
{
reverse_instructions(ic);
swap_last_operands(vm.instructions[0]);
vm.instructions[ic] = OP_END; // append end instruction
interpret(map);
}

return;
}
3 changes: 2 additions & 1 deletion src/concoct.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ void lex_string(const char* input_string)
//const char* input = "func test() { return a + b }";
ConcoctCharStream* char_stream = cct_new_string_char_stream(input_string);
ConcoctLexer* string_lexer = cct_new_lexer(char_stream);

if(string_lexer == NULL)
{
fprintf(stderr, "String lexer is NULL!\n");
Expand Down Expand Up @@ -376,6 +376,7 @@ void interactive_mode()
{
memset(input, 0, sizeof(input)); // reset input every iteration
printf("> ");
fflush(stdout);
if(fgets(input, 1024, stdin) == NULL)
{
puts("");
Expand Down
5 changes: 4 additions & 1 deletion src/tests/interpret_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
*/

#include "debug.h"
#include "hash_map.h"
#include "memory.h"
#include "vm/vm.h"

Expand All @@ -34,6 +35,7 @@ int main()
Object* object = NULL;
void* vptr = NULL;
BigNum numval = -8675309;
ConcoctHashMap* map = cct_new_hash_map(INITIAL_BUCKET_AMOUNT);
debug_mode = true;
init_vm();

Expand Down Expand Up @@ -81,7 +83,8 @@ int main()
vm.instructions[17] = OP_CLS;
vm.instructions[18] = OP_END;

interpret();
interpret(map);
cct_delete_hash_map(map);
stop_vm();

return 0;
Expand Down
Loading

0 comments on commit 33b93cf

Please sign in to comment.