Skip to content

Instantly share code, notes, and snippets.

@maximvl
maximvl / abc-score.red
Last active September 27, 2017 03:59
abc score for Red code
Red [
author: {Maxim Velesyuk}
usage: {
abc-score? <block of code>
abc-score? %file.red
abc-score? :some-function
}
description: {
The ABC software metric defines an ABC score as a triplet of values that represent the size of a set of source code statements.
An ABC score is calculated by counting the number of assignments (A), number of branches (B), and number of conditionals (C) in a program.
>> mal/parser/run "{1 2}"
-->
match: [(error: false) any whitespace collect [ahead "("
input: "{1 2}"
match: [any whitespace collect [ahead "(" mal-list (probe
input: "{1 2}"
-->
==> matched
<--
match: [whitespace collect [ahead "(" mal-list (probe "fo
@maximvl
maximvl / info.red
Created September 4, 2017 08:50 — forked from toomasv/info.red
To get info about any function
Red []
info: func ['fn /name /intro /args /refinements /locals /return /spec
/arg-num /arg-names /arg-types /ref-names /ref-types /ref-num /type
/local intr ars refs locs ret arg ref typ
][
intr: copy "" ars: make map! copy [] refs: make map! copy [] locs: copy [] ret: copy [] typ: ref-arg: ref-arg-type: none
if lit-word? fn [fn: to-word fn]
unless find [op! native! function! action!] type?/word get fn [
cause-error 'user 'message ["Only function types accepted!"]
]
@maximvl
maximvl / python.red
Last active May 12, 2022 03:58
python game
Red [
author: {Maxim Velesyuk}
description: {
A Snake Game / basic version
specification by Edward de Jong, version 1
The game runs at 6 frames per second, and the snake moves one cell per frame. If the snake moves into the apple the length is increased by one, a crunch sound is emitted, and the apple is moved to a new cell. If the snake crosses over itself, then a beep is emitted and the part of the snake from that cell onward is erased. The snake wraps around the board in all four directions.
The playing board, drawn solid black, is subdivided into square cells of 42 points at the screen resolution. Since the window size will not be an even number of cells, the cells are stretched slightly so that all screen space is used by the grid. At the start, the snake is set to length 1 and positioned at cell (4,4). The apple, which is the goal of the snake to eat, is one cell drawn as a circle in HTML color crimson, and is placed at a random location on the board.
At the start of the game the snake is paused. A
Red [
author: { Maxim Velesyuk }
description: {
colors palette showcase
}
]
sys-words: words-of system/words
colors: copy []
forall sys-words [
@maximvl
maximvl / state.red
Created July 22, 2017 23:28
Red parse + states example
Red []
parser: context [
state: []
rules: [
any [
number! (append state 'number) |
word! (append state 'word) |
time! (append state 'time)
]
@maximvl
maximvl / html-parser.red
Created July 22, 2017 10:16
toy html parser
Red []
{
grammar HTML
document <- (doctype / text / tag)*
tag <- open_tag (text / tag)* close_tag
open_tag <- "<" [0-9a-zA-Z \"'=-]+ ">"
close_tag <- "</" [0-9a-zA-Z]+ ">"
doctype <- "<!DOCTYPE " [0-9a-zA-Z]+ ">"
text <- [^<]+
}
@maximvl
maximvl / qf.red
Created May 15, 2017 21:51
quick funcs
Red [
author: {Maxim Velesyuk}
description: {
Quick in-place function definitions without explicit arguments specification.
`_` is next arugment, `_N` is N-th argument
}
]
inc: func ['word] [set word 1 + get word]
Red [
author: {Maxim Velesyuk}
description: {draw dialect playground}
]
demo: trim {
fill-pen gray
box 0x0 400x400
do [c: to-tuple reduce [
random 255 random 255 random 255]
* (defun f (x y) (/ x y))
WARNING: redefining COMMON-LISP-USER::F in DEFUN
F
* (f 5 0)
debugger invoked on a DIVISION-BY-ZERO in thread
#<THREAD "main thread" RUNNING {100399C493}>:
arithmetic error DIVISION-BY-ZERO signalled
Operation was /, operands (5 0).