Merge pull request #51 from Yardanico/addnim

Add Nim demo
This commit is contained in:
Henning Dieterichs
2020-05-11 10:22:55 +02:00
committed by GitHub
5 changed files with 133 additions and 0 deletions

18
demos/nim/.vscode/launch.json vendored Normal file
View File

@@ -0,0 +1,18 @@
{
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Run your Executable",
"program": "${workspaceFolder}/main",
"args": [],
"cwd": "${workspaceFolder}",
"initCommands": [
// To let LLDB know that NCSTRING is just like a C string
"type format add --format c-string NCSTRING"
],
"preLaunchTask": "build"
}
]
}

10
demos/nim/.vscode/tasks.json vendored Normal file
View File

@@ -0,0 +1,10 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "nim c main.nim"
}
]
}

21
demos/nim/LICENSE Executable file
View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2020 Danil Yarantsev
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

82
demos/nim/main.nim Normal file
View File

@@ -0,0 +1,82 @@
import random, json, options, sugar
type
Kind = object
array: bool
Label = object
label: Option[string]
Column = object
content: Option[string]
tag: Option[string]
color: Option[string]
Row = object
label: Option[string]
columns: seq[Column]
Marker = object
id: string
row: int
column: int
rows: Option[int]
columns: Option[int]
label: Option[string]
color: Option[string]
Grid = object
kind: Kind
columnLabels: Option[seq[Label]]
rows: seq[Row]
markers: Option[seq[Marker]]
proc showSeq[T](data: seq[T], markers: seq[(string, int)] = @[]): cstring =
# Need to use cstring string type for C-compatible strings
let labels = collect(newSeq):
# Labels are indexes of the seq
for x in 0 ..< data.len():
Label(label: some($x))
let columns = collect(newSeq):
# Columns contain values of the seq
for x in data:
Column(content: some($x), tag: some($x))
let marks = collect(newSeq):
# If there are any markers (id - string, index - int)
for marker in markers:
Marker(id: marker[0], row: 0, column: marker[1])
let grid = Grid(
kind: Kind(array: true),
rows: @[Row(columns: columns)],
columnLabels: some(labels),
markers: some(marks)
)
# Serialize to JSON
result = $ %grid
proc main =
# Random seed
randomize()
# Fill seq with 10 random values from 1 to 999
var a = newSeq[int]()
for x in 0..10:
a.add rand(1..999)
# Simple bubble sort with some markers
# Open the visualizer and type in "ress"
let n = a.len() - 1
var ress = showSeq(a, @[("i", 0), ("j", 0)])
for i in 0 .. n:
for j in 0 .. n - i - 1:
if a[j] > a[j+1]:
ress = showSeq(a, @[("i", i), ("j", j)]) # breakpoint
let temp = a[j]
a[j] = a[j + 1]
a[j + 1] = temp
ress = showSeq(a, @[("i", i), ("j", j)]) # breakpoint
main()

2
demos/nim/nim.cfg Normal file
View File

@@ -0,0 +1,2 @@
# For Nim source code lines
debugger:native