Move helloWorld example up onto cli-platform

This commit is contained in:
Jan Van Bruggen 2022-09-11 21:46:04 -06:00
parent 8fbd09b9ea
commit 2eec200f09
No known key found for this signature in database
GPG key ID: FE2A4E38E0FA6134
12 changed files with 14 additions and 127 deletions

View file

@ -28,7 +28,7 @@ jobs:
run: ls | grep "roc_nightly.*tar\.gz" | xargs tar -xzvf
- name: test roc hello world
run: ./roc examples/hello-world/main.roc
run: ./roc examples/helloWorld.roc

View file

@ -41,7 +41,7 @@ jobs:
run: ls | grep "roc_nightly.*tar\.gz" | xargs tar -xzvf
- name: test roc hello world
run: ./roc examples/hello-world/main.roc
run: ./roc examples/helloWorld.roc

View file

@ -1,4 +1,4 @@
#!/usr/bin/env bash
cp target/release/roc ./roc # to be able to exclude "target" later in the tar command
cp -r target/release/lib ./lib
tar -czvf $1 --exclude="target" --exclude="zig-cache" roc lib LICENSE LEGAL_DETAILS examples/hello-world crates/roc_std
tar -czvf $1 --exclude="target" --exclude="zig-cache" roc lib LICENSE LEGAL_DETAILS examples/helloWorld.roc examples/cli crates/roc_std

View file

@ -523,7 +523,7 @@ fn read_main_roc_file(project_dir_path_opt: Option<&Path>) -> (PathBuf, String)
// returns path and content of app file
fn init_new_roc_project(project_dir_path: &Path) -> (PathBuf, String) {
let orig_platform_path = Path::new("./examples/hello-world").join(PLATFORM_DIR_NAME);
let orig_platform_path = Path::new("./examples/interactive").join(PLATFORM_DIR_NAME);
let roc_file_path = Path::new("./new-roc-project/main.roc");

2
examples/.gitignore vendored
View file

@ -4,3 +4,5 @@ libapp.so
dynhost
preprocessedhost
metadata
helloWorld

View file

@ -1 +0,0 @@
helloWorld

View file

@ -1,16 +0,0 @@
# Hello, World!
To run, `cd` into this directory and run this in your terminal:
```bash
roc run
```
This will run `main.roc` because, unless you explicitly give it a filename, `roc run`
defaults to running a file named `main.roc`. Other `roc` commands (like `roc build`, `roc test`, and so on) also default to `main.roc` unless you explicitly give them a filename.
## About this example
This uses a very simple platform which does nothing more than printing the string you give it.
The line `main = "Hello, World!\n"` sets this string to be `"Hello, World!"` with a newline at the end, and the lines `packages { pf: "platform/main.roc" }` and `provides [main] to pf` specify that the `platform/` directory contains this app's platform.

View file

@ -1,6 +0,0 @@
app "helloWorld"
packages { pf: "platform/main.roc" }
imports []
provides [main] to pf
main = "Hello, World!\n"

View file

@ -1,89 +0,0 @@
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
void* roc_alloc(size_t size, unsigned int alignment) { return malloc(size); }
void* roc_realloc(void* ptr, size_t new_size, size_t old_size,
unsigned int alignment) {
return realloc(ptr, new_size);
}
void roc_dealloc(void* ptr, unsigned int alignment) { free(ptr); }
void roc_panic(void* ptr, unsigned int alignment) {
char* msg = (char*)ptr;
fprintf(stderr,
"Application crashed with message\n\n %s\n\nShutting down\n", msg);
exit(0);
}
void* roc_memcpy(void* dest, const void* src, size_t n) {
return memcpy(dest, src, n);
}
void* roc_memset(void* str, int c, size_t n) { return memset(str, c, n); }
struct RocStr {
char* bytes;
size_t len;
size_t capacity;
};
bool is_small_str(struct RocStr str) { return ((ssize_t)str.capacity) < 0; }
// Determine the length of the string, taking into
// account the small string optimization
size_t roc_str_len(struct RocStr str) {
char* bytes = (char*)&str;
char last_byte = bytes[sizeof(str) - 1];
char last_byte_xored = last_byte ^ 0b10000000;
size_t small_len = (size_t)(last_byte_xored);
size_t big_len = str.len;
// Avoid branch misprediction costs by always
// determining both small_len and big_len,
// so this compiles to a cmov instruction.
if (is_small_str(str)) {
return small_len;
} else {
return big_len;
}
}
extern void roc__mainForHost_1_exposed_generic(struct RocStr *string);
int main() {
struct RocStr str;
roc__mainForHost_1_exposed_generic(&str);
// Determine str_len and the str_bytes pointer,
// taking into account the small string optimization.
size_t str_len = roc_str_len(str);
char* str_bytes;
if (is_small_str(str)) {
str_bytes = (char*)&str;
} else {
str_bytes = str.bytes;
}
// Write to stdout
if (write(1, str_bytes, str_len) >= 0) {
// Writing succeeded!
// NOTE: the string is a static string, read from in the binary
// if you make it a heap-allocated string, it'll be leaked here
return 0;
} else {
printf("Error writing to stdout: %s\n", strerror(errno));
// NOTE: the string is a static string, read from in the binary
// if you make it a heap-allocated string, it'll be leaked here
return 1;
}
}

View file

@ -1,9 +0,0 @@
platform "hello-world"
requires {} { main : Str }
exposes []
packages {}
imports []
provides [mainForHost]
mainForHost : Str
mainForHost = main

6
examples/helloWorld.roc Normal file
View file

@ -0,0 +1,6 @@
app "helloWorld"
packages { pf: "interactive/cli-platform/main.roc" }
imports [pf.Stdout]
provides [main] to pf
main = Stdout.line "Hello, World!"

View file

@ -23,8 +23,8 @@ If you have a specific question, the [FAQ](../FAQ.md) might have an answer, alth
You can run examples as follows:
```sh
cd examples/hello-world
roc run
cd examples
roc run helloWorld.roc
```
Some examples like `examples/benchmarks/NQueens.roc` require input after running.