Skip to content

Commit

Permalink
[WIP] RScript Support (#345)
Browse files Browse the repository at this point in the history
* Create main.r

Signed-off-by: Ian Hunter <[email protected]>

* Create test_R.yml

Signed-off-by: Ian Hunter <[email protected]>

* Create target.mk

Signed-off-by: Ian Hunter <[email protected]>

* Update target.mk

Signed-off-by: Ian Hunter <[email protected]>

* Update Makefile

Signed-off-by: Ian Hunter <[email protected]>

* Update test_R.yml

Signed-off-by: Ian Hunter <[email protected]>

* Update test_R.yml

Signed-off-by: Ian Hunter <[email protected]>

* Update test_R.yml

Signed-off-by: Ian Hunter <[email protected]>

* Update test_R.yml

Signed-off-by: Ian Hunter <[email protected]>

* Update test_R.yml

Signed-off-by: Ian Hunter <[email protected]>

* Update target.mk

Signed-off-by: Ian Hunter <[email protected]>

* Update target.mk

Signed-off-by: Ian Hunter <[email protected]>

* Update main.r

Signed-off-by: Ian Hunter <[email protected]>

* Update target.mk

Signed-off-by: Ian Hunter <[email protected]>

* Update main.r

Signed-off-by: Ian Hunter <[email protected]>

* Update main.r

Signed-off-by: Ian Hunter <[email protected]>

* Update target.mk

Signed-off-by: Ian Hunter <[email protected]>

* Update target.mk

Signed-off-by: Ian Hunter <[email protected]>

* Update target.mk

Signed-off-by: Ian Hunter <[email protected]>

* Update target.mk

Signed-off-by: Ian Hunter <[email protected]>

* Update main.r

Signed-off-by: Ian Hunter <[email protected]>

* Update main.r

Signed-off-by: Ian Hunter <[email protected]>

* Update shared_header.h

Signed-off-by: Ian Hunter <[email protected]>

* Update dice.yacc

Signed-off-by: Ian Hunter <[email protected]>

* Update main.r

Signed-off-by: Ian Hunter <[email protected]>

* Update main.r

Signed-off-by: Ian Hunter <[email protected]>

* Update main.r

Signed-off-by: Ian Hunter <[email protected]>

* Update main.r

Signed-off-by: Ian Hunter <[email protected]>

* Update main.r

Signed-off-by: Ian Hunter <[email protected]>

* Update main.r

Signed-off-by: Ian Hunter <[email protected]>

* Update main.r

Signed-off-by: Ian Hunter <[email protected]>

* Update main.r

Signed-off-by: Ian Hunter <[email protected]>

* Update main.r

Signed-off-by: Ian Hunter <[email protected]>

* Update main.r

Signed-off-by: Ian Hunter <[email protected]>

* Update main.r

Signed-off-by: Ian Hunter <[email protected]>

* working R

* update readmes

Signed-off-by: Ian Hunter <[email protected]>
  • Loading branch information
ianfhunter authored Nov 9, 2022
1 parent 76a44dd commit dd486b7
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 2 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/test_R.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: "Test: R"

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
build:
runs-on: ubuntu-latest


steps:
- uses: actions/checkout@v3
- uses: r-lib/actions/setup-r@v2
- name: make r
run: make r
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,7 @@ src/go/main
*.stackdump

# TROLL
test.t
test.t

# GNOLL Outputs
*.dice
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
[![Test: JavaScript](https://github.com/ianfhunter/GNOLL/actions/workflows/test_JavaScript.yml/badge.svg)](https://github.com/ianfhunter/GNOLL/actions/workflows/test_JavaScript.yml)
[![Test: Go](https://github.com/ianfhunter/GNOLL/actions/workflows/test_Go.yml/badge.svg)](https://github.com/ianfhunter/GNOLL/actions/workflows/test_Go.yml)
[![Test: Julia](https://github.com/ianfhunter/GNOLL/actions/workflows/test_julia.yml/badge.svg)](https://github.com/ianfhunter/GNOLL/actions/workflows/test_julia.yml)
[![Test: R](https://github.com/ianfhunter/GNOLL/actions/workflows/test_R.yml/badge.svg)](https://github.com/ianfhunter/GNOLL/actions/workflows/test_R.yml)

[![CodeFactor](https://www.codefactor.io/repository/github/ianfhunter/gnoll/badge)](https://www.codefactor.io/repository/github/ianfhunter/gnoll)
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/90add1388135474a928b715ddbb071b4)](https://www.codacy.com/gh/ianfhunter/GNOLL/dashboard?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=ianfhunter/GNOLL&amp;utm_campaign=Badge_Grade)
Expand Down Expand Up @@ -44,6 +45,7 @@ We wrote GNOLL to be the definitive solution to dice notation. We've written all
- Go
- JavaScript (Node.js)
- Julia
- R

We have plans to add more example integrations of GNOLL's through different languages in the near future. Let us know if you have any particular wants!

Expand Down
17 changes: 17 additions & 0 deletions src/R/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# R Support

## Setup
Generate the shared object file that R can consume.
```bash
make r
```

Inside `main.r` we show an example of GNOLL usage.

- Load in the shared object
- Delete the temporary file that contains GNOLL output
- Call GNOLL
- Parse result

## Notes
This example uses the .C function which is usually not recommended. The recommended way to call C code in R is through the .Call() function. See [#368](https://github.com/ianfhunter/GNOLL/issues/368)
33 changes: 33 additions & 0 deletions src/R/main.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# print("Hello GNOLL")

dyn.load("build/r/dice.so")

fn = "output.dice"
if(file.exists(fn)){
file.remove(fn)
}

return_code = as.integer(-1)
error_code = .C(
"roll_and_write_R",
value=return_code,
"1d20",
fn
)

if(error_code$value != 0){
print(paste("GNOLL Error Code:", error_code$value))
stop("GNOLL Exit code was not Success")
}

result <- readLines(fn, warn=FALSE)
tokens <- strsplit(result, ";")
int_tokens <- strtoi(tokens)

print(paste("GNOLL rolled:", result))

if(int_tokens[1] <= 0 | int_tokens[1] > 20){
stop ("GNOLL rolled invalid value")
}


6 changes: 6 additions & 0 deletions src/R/target.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.PHONY: r

r: clean yacc lex compile $(OBJECTS)
mkdir -p build/r/
$(CC) -shared -o build/r/dice.so $(OBJECTS) $(ARC4RANDOM) -lR
Rscript src/R/main.r
7 changes: 6 additions & 1 deletion src/grammar/dice.yacc
Original file line number Diff line number Diff line change
Expand Up @@ -1249,7 +1249,7 @@ int roll(char * s){
return gnoll_errno;
}

int roll_and_write(char * s, char * f){
int roll_and_write(char* s, char* f){
gnoll_errno = 0;
write_to_file = 1;
output_file = f;
Expand All @@ -1258,6 +1258,11 @@ int roll_and_write(char * s, char * f){
/* free(macros); */
return return_code;
}

void roll_and_write_R(int* return_code, char** s, char** f){
(*return_code) = roll_and_write(s[0], f[0]);
}

int mock_roll(char * s, char * f, int mock_value, int mock_const){
gnoll_errno = 0;
init_mocking((MOCK_METHOD)mock_value, mock_const);
Expand Down
1 change: 1 addition & 0 deletions src/grammar/shared_header.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ typedef enum {

int roll(char* s);
int roll_and_write(char* s, char* f);
void roll_and_write_R(int* return_code, char** s, char** f );
int mock_roll(char* s, char* f, int mock_value, int mock_const);

// #ifdef __cplusplus
Expand Down
2 changes: 2 additions & 0 deletions src/julia/GNOLL/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
[![Test: JavaScript](https://github.com/ianfhunter/GNOLL/actions/workflows/test_JavaScript.yml/badge.svg)](https://github.com/ianfhunter/GNOLL/actions/workflows/test_JavaScript.yml)
[![Test: Go](https://github.com/ianfhunter/GNOLL/actions/workflows/test_Go.yml/badge.svg)](https://github.com/ianfhunter/GNOLL/actions/workflows/test_Go.yml)
[![Test: Julia](https://github.com/ianfhunter/GNOLL/actions/workflows/test_julia.yml/badge.svg)](https://github.com/ianfhunter/GNOLL/actions/workflows/test_julia.yml)
[![Test: R](https://github.com/ianfhunter/GNOLL/actions/workflows/test_R.yml/badge.svg)](https://github.com/ianfhunter/GNOLL/actions/workflows/test_R.yml)


[![CodeFactor](https://www.codefactor.io/repository/github/ianfhunter/gnoll/badge)](https://www.codefactor.io/repository/github/ianfhunter/gnoll)
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/90add1388135474a928b715ddbb071b4)](https://www.codacy.com/gh/ianfhunter/GNOLL/dashboard?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=ianfhunter/GNOLL&amp;utm_campaign=Badge_Grade)
Expand Down
1 change: 1 addition & 0 deletions src/python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
[![Test: JavaScript](https://github.com/ianfhunter/GNOLL/actions/workflows/test_JavaScript.yml/badge.svg)](https://github.com/ianfhunter/GNOLL/actions/workflows/test_JavaScript.yml)
[![Test: Go](https://github.com/ianfhunter/GNOLL/actions/workflows/test_Go.yml/badge.svg)](https://github.com/ianfhunter/GNOLL/actions/workflows/test_Go.yml)
[![Test: Julia](https://github.com/ianfhunter/GNOLL/actions/workflows/test_julia.yml/badge.svg)](https://github.com/ianfhunter/GNOLL/actions/workflows/test_julia.yml)
[![Test: R](https://github.com/ianfhunter/GNOLL/actions/workflows/test_R.yml/badge.svg)](https://github.com/ianfhunter/GNOLL/actions/workflows/test_R.yml)[![Test: R](https://github.com/ianfhunter/GNOLL/actions/workflows/test_R.yml/badge.svg)](https://github.com/ianfhunter/GNOLL/actions/workflows/test_R.yml)

[![CodeFactor](https://www.codefactor.io/repository/github/ianfhunter/gnoll/badge)](https://www.codefactor.io/repository/github/ianfhunter/gnoll)
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/90add1388135474a928b715ddbb071b4)](https://www.codacy.com/gh/ianfhunter/GNOLL/dashboard?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=ianfhunter/GNOLL&amp;utm_campaign=Badge_Grade)
Expand Down

0 comments on commit dd486b7

Please sign in to comment.