Skip to content

Instantly share code, notes, and snippets.

View spytheman's full-sized avatar
🏠
Working from home

Delyan Angelov spytheman

🏠
Working from home
View GitHub Profile
@spytheman
spytheman / tcp_echo_server.v
Last active March 28, 2020 18:19
A simple tcp echo server written in the V programming language.
import net
// Connect with: `nc 127.0.0.1 12345`
fn handle_connection(con net.Socket) {
eprintln('new client connected')
defer { eprintln('closing connection: $con') con.close() or { } }
con.send_string("Welcome to V's TCP Echo server.\n") or { return }
for {
line := con.read_line()
if line.len == 0 { return }
eprintln('received line: ' + line.trim_space())
import v.ast
import v.table
import v.token
fn s(x ast.Stmt) ast.Stmt { return x }
fn es(x ast.Expr) ast.Expr { return x }
fn stmts_last_expr_opt(stmts []ast.Stmt) ?ast.ExprStmt {
le := stmts[ stmts.len - 1 ]
match le {
import os
import rand
import time
import encoding.base64
fn main() {
rand.seed(time.sys_mono_now())
mut ascii_data_output := ''
mut key := ''
process_menu := os.input('Enter the process you would like to occur:\n1. Encipher \n2. Decipher\n')
@spytheman
spytheman / tetris.v
Last active June 18, 2020 18:52
V tetris, without font rendering/usage.
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module main
import rand
import time
import gx
import gg
/*
** 2001-09-15
**
** The author disclaims copyright to this source code. In place of
** a legal notice, here is a blessing:
**
** May you do good and not evil.
** May you find forgiveness for yourself and forgive others.
** May you share freely, never taking more than you give.
**
@spytheman
spytheman / rand_uuid4_compare.v
Last active July 25, 2020 17:08
a small demo of vlang vlib/rand, vlib/benchmark, and rand.uuid_v4
import os
import os.cmdline
import rand
import benchmark
//
// Example run with:
// ./v -prod -cc tcc run rand_uuid4_compare.v -repetitions 1000000
// ./v -prod -cc gcc-9 run rand_uuid4_compare.v -repetitions 1000000
// ./v -prod -cc clang-10 run rand_uuid4_compare.v -repetitions 1000000
@spytheman
spytheman / rand_uuid_v4__vs__rand_ulid.v
Last active July 26, 2020 07:31
rand.uuid_v4() vs rand.ulid()
import os
import os.cmdline
import rand
import benchmark
//
// Example run with:
// ./v -prod -cc tcc run rand_uuid_v4__vs__rand_ulid.v -repetitions 1000_000
// ./v -prod -cc gcc-9 run rand_uuid_v4__vs__rand_ulid.v -repetitions 1000_000
// ./v -prod -cc clang-10 run rand_uuid_v4__vs__rand_ulid.v -repetitions 1000_000
type SuccessCode int
type FailureCode int
type Code = SuccessCode | FailureCode
fn (x SuccessCode) hex() string { return 'my hex: ' + int(x).hex() }
//
fn (x SuccessCode) str() string { return 'SC(' + int(x).str() +')' }
fn (x FailureCode) str() string { return 'FC(' + int(x).str() +')' }
fn (x Code) str() string {
mut res := ''
import os
import json
struct User { name string age int = 30 }
fn main(){
u := User{ name: os.args[1..].join(' ') }
smessage := json.encode(u)
C.write(1, &smessage.len, 4)
C.write(1, smessage.str, smessage.len)
}
import json
struct User { name string age int }
fn main(){
mut len := 0
C.read(0, &len, 4)
if len > 1024 { panic('input too large') }
mut buf := malloc(len)
C.read(0, buf, len)
s := string(buf, len)
println('received s: $s')