How to properly use the Red/System dialect from the pure Red code? - red

I am writing a simple tutorial application using curses binding to Red/System. Doing it like it is shown in "curses-example.reds" works great. But how can I use this binding from a pure Red code? Did I get the whole approach wrong and I really must stay within borders of Red/System dialect?
The code is as simple as it could be:
Red/System [
File: "%test.reds"
]
#include %curses/curses.reds
with curses [
initscr
getch
endwin
]
How I can properly do a two-way date exchange between Red/System and Red?
I did not get it pretty good from the answer to this question.

Currently, to use Red/System Code from a Red program you need to use a routine! - http://www.red-lang.org/search/label/routine
At the moment, only integer! and logic! values can be passed transparently between Red and Red/System. Other datatypes have to be converted, inside the routine, to be usable.
For example, if you have a string! datatype as a parameter of a routine, Red will pass the data to the routine as a red-string! structure:
red-string!: alias struct! [
header [integer!] ;-- cell header
head [integer!] ;-- string's head index (zero-based)
node [node!] ;-- series node pointer
cache [c-string!] ;-- (experimental)
]
It needs to be converted to a Red/System c-string! before you can use it in the routine. At the same time, you must cater for the encoding differences between Red string!s and Red/System c-string!s. A red string! may be ISO-8559-1, UCS-2 or UTF-32 encoded.
You must cater for the opposite if you wish to return a string! to a Red program from a routine.
Other datatypes will need to be handled in similar ways.
I'm sure that passing values between Red and Red/System will be made much, much easier in future. However, I wouldn't personally expect that until after Red reaches version 1.0.

First make your code Red code not Red/System. Next try to stay as close as possible to the examples from the blog on the red-lang.org site.
You need to look for routine.
I want to say a special thank you to the person that has voted my answer down.

Related

Does VSCode snippet syntax allow optional parameters to a command

I recently started using a 3rd party extension for language supporting robot programming (RoboDK). The extension is pretty good but doesn't include many of the commands I would like to use and doesn't correctly (at least to me) handle optional arguments to commands. I'd like to 'extend' the extension to support additional commands (and potentially provide this feedback to the developer).
I'm specifically using ABB's RAPID robot language.
Example:
MoveL robtarget, speeddata, zonedata, tooldata \WObj:=WObj0;
For MoveL, var1 thru var4 are required but \WObj:=WObj0 is optional. The '\WObj0:=' part of the string is a constant (if supplying the option) and WObj0 would be a variable of the expected type for MoveL (a work object, in this case).
Is there a way to edit the snippets json to not enter the optional part unless I start typing a backslash followed by text? I also would like to keep the optional part in the snippet as a reference so I don't have to remember every optional argument as some commands have many options.
Here's an excerpt from the snippet.abb.json file:
"movel": {
"prefix": "movel",
"body": [
"MoveL ${1:var_robtarget}, ${2:var_speeddata}, ${3:var_zonedata}, ${4:pers_tooldata} \\WObj:=WObj0;"
],
"description": "Linear motion"
},
Which results in the code with var_robtarget, var_speeddata, var_zonedata & pers_tooldata all highlighted and the remainder is plain text:
MoveL var_robtarget, var_speeddata, var_zonedata, pers_tooldata \WObj:=WObj0;
Is there a way to edit the snippets json to not enter the optional
part unless I start typing a backslash followed by text?
No, I don't think there is any way to do that within the same snippet. You could make another snippet with the backslash prefix but that is cumbersome.
Here is a simple way to do what you want but you will have to delete any optional variables you don't want:
"body" [
"MoveL ${1:var_robtarget}, ${2:var_speeddata}, ${3:var_zonedata}, ${4:pers_tooldata}${5: \\WObj:=WObj0}${6: anotherVar};"
]
That last variables are also selected when you get to them. If you don't want any just hit Delete.
In the above demo I am just tabbing and deleting.

Uniform list pretty-printer

It is known that default printer can be confusing wrt lists because of no output for empty lists and 3 different notations being mixed (, vs (x;y;z) vs 1 2 3) and not obvious indentation/columnization (which is apparently optimized for table data). I am currently using -3! but it is still not ideal.
Is there a ready-made pretty-printer that has consistent uniform output format (basically what I am used to in any other language where list is not special)?
I've started using .j.j for string outputs in error messages more recently in preference to -3!. Mainly I think it is easier to parse in a text log, but also doesn't truncate in the same way.
It still transforms atoms and lists differently so it might not exactly meet your needs, if you really want that you could compose it with the old "ensure this is a list" trick:
myPrinter:('[.j.j;(),])
You might need to supply some examples to better explain your issues and your use-case for pretty-printing.
In general -3! is the most clear visual representation of the data. It is the stringified equivalent to another popular display method which is 0N!.
The parse function is useful for understanding how the interpreter reads/executes commands but I don't think that will be useful in your case

Netlogo arrays need literal values

The array is expecting a literal value
set chrom [forage_min forage_rate share_min share_rate mating_treshold]
print chrom
How can I handle it? I really don't understand arrays in Netlogo.
(You speak of "arrays" in your question, but I think you mean "lists". It is possible to use arrays in NetLogo via the array extension, but unless you have very specific needs, that's probably not what you want. So, assuming that you are trying to create a list:)
The square bracket syntax for declaring lists only works with "literal" values, e.g., raw strings or numbers. If you want to build a list out of variables or more complex expressions, you need to use the list primitive. In your case, that would be something like:
set chrom (list forage_min forage_rate share_min share_rate mating_treshold)
I would encourage you to read the Lists section of the NetLogo programming guide.

Safe.pm base_math as math calculator and new variables

I do not want to write my own recursive-descent math parser or think too deeply about grammar, so I am (re-)using the Perl module Safe.pm as an arithmetic calculator with variables. My task is to let one anonymous web user A type into a textfield a couple of math expressions, like:
**Input Formula:** $x= 2; $y=sqrt(2*$x+(25+$x)*$x); $z= log($y); ...
Ideally, this should only contain math expressions, but not generic Perl code. Later, I want to use it for web user B:
**Input Print:** you start with x=$x and end with z=$z . you don't know $a.
to <pre> text output that looks like this:
**Output Txt:** you start with x=2 and end with z=2.03 . you don't know $a.
(The fact that $a was not replaced is its own warning.) Ideally, I want to check that my web users have not only not tried to break in, but also have made no syntax errors.
My current Safe.pm-based implementation has drawbacks:
I want only math expressions in the first textfield. Alas, :base_math only extends Safe.pm beyond :base_core, so I have to live with the user having access to more than just math algebra expressions. For example, the web users could accidentally try to use a Perl reserved name, define subs, or do who knows what. Is there a better solution that picks off only the recursive descent math grammar parser? (and, subs like system() should not be permitted math functions!)
For the printing, I can just wrap a print "..." around the text and go another Safe eval, but this replaces $a with undef. What I really mean my code to do is to go through the table of newly added variables ($x, $y, and $z) and if they appear unescaped, then replace them; others should be ignored. I also have to watch carefully here that my guys are not working together to try to escape and type text like "; system("rm -rf *"); print ";, though Safe would catch this particular issue. More likely, A could try to inject some nasty JavaScript for B or who knows what.
Questions:
Is Safe.pm the right tool for the job? Perl seems like a heavy cannon here, but not having to reinvent the wheel is nice.
Can one further restrict Safe.pm to Perl's arithmetic only?
Is there a "new symbols" table that I can iterate over for substitution?
Safe.pm seems like a bad choice, because you're going to run the risk of overlooking some exploitable operation. I would suggest looking at a parsing tool, such as Marpa. It even has the beginnings of a calculator implementation which you could probably adapt to your purposes.

In a function show passed arguments in a different color [duplicate]

In Emacs, is it possible to mark all variables of different data types with different colors? e.g. if I have the following variables in C/C++ my program
int i,j;
float g,h;
char a,b;
Then throughout the source code i and j would be marked as red, g and h as green, a and b as blue.
I am not sure how useful this will be in future, but I feel it would help me while reading code,
and be a good alternative to the Hungarian notation(not that I use this notation :D).
No. Emacs has no idea about the type of a specific expression; doing this would be tantamount to writing a significant part of a C compiler in ELisp.
However, there is a light at the end of the tunnel.
E.g., if you edit OCaml code using tuareg-mode, you can ask Emacs about the type of any expression because the ocaml compiler provides that information; thus you should be able to ask it to highlight variables by type. This is the path to follow.
Alas, gcc does not provide that information; however, its extensiongccxml does.
Also, other C compilers, e.g., clang, provide that information out of the box, and there is a new file semantic-clang.el which relies on those features (although for completion only, not for syntax highlighting).
So, nothing out of the box for you here, but if you are willing to use clang instead of gcc and contribute to the CEDET development, you might get what you want.
No, it's not possible to selectively assign a given color to a given variable in emacs (or just for one given program).
However, if it's just syntax highlighting you are looking for, of course, emacs will highlight most languages, and you can even create syntax highlighting for languages emacs would not know about.
Ex. Smali: https://github.com/strazzere/Emacs-Smali