Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for V #4564

Merged
merged 6 commits into from
Aug 12, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,9 @@
[submodule "vendor/grammars/vscode-slice"]
path = vendor/grammars/vscode-slice
url = https://github.com/zeroc-ice/vscode-slice
[submodule "vendor/grammars/vscode-vlang"]
path = vendor/grammars/vscode-vlang
url = https://github.com/0x9ef/vscode-vlang
[submodule "vendor/grammars/vscode-zil-language"]
path = vendor/grammars/vscode-zil-language
url = https://github.com/tclem/vscode-zil-language
Expand Down
2 changes: 2 additions & 0 deletions grammars.yml
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,8 @@ vendor/grammars/vscode-scala-syntax:
- source.scala
vendor/grammars/vscode-slice:
- source.slice
vendor/grammars/vscode-vlang:
- source.v
vendor/grammars/vscode-zil-language:
- source.zap
- source.zil
Expand Down
8 changes: 8 additions & 0 deletions lib/linguist/heuristics.yml
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,14 @@ disambiguations:
pattern: '^\s*(import.+(from\s+|require\()[''"]react|\/\/\/\s*<reference\s)'
- language: XML
pattern: '(?i:^\s*<\?xml\s+version)'
- extensions: ['.v']
rules:
- language: Coq
pattern: '\(\*.*?\*\)|(?:^|\s)(?:Proof|Qed)\.(?:$|\s)|(?:^|\s)Require[ \t]+Import\s'
- language: Verilog
pattern: '^[ \t]*module\s+[^\s()]+\s+\#?\(|^[ \t]*`(?:ifdef|timescale)\s|^[ \t]*always[ \t]+@'
- language: V
pattern: '\$(?:if|else)[ \t]|^[ \t]*fn\s+[^\s()]+\(.*?\).*?\{|^[ \t]*for\s*\{'
- extensions: ['.vba']
rules:
- language: Vim script
Expand Down
12 changes: 12 additions & 0 deletions lib/linguist/languages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5274,6 +5274,18 @@ UrWeb:
tm_scope: source.ur
ace_mode: text
language_id: 383
V:
type: programming
color: "#5d87bd"
aliases:
- vlang
extensions:
- ".v"
tm_scope: source.v
ace_mode: golang
codemirror_mode: go
codemirror_mime_type: text/x-go
language_id: 603371597
VCL:
type: programming
color: "#148AA8"
Expand Down
38 changes: 38 additions & 0 deletions samples/V/json.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import json

struct User {
name string
age int
mut:
is_registered bool
}

fn main() {
s := '[{ "name":"Frodo", "age":25}, {"name":"Bobby", "age":10}]'
users := json.decode( []User, s) or {
eprintln('Failed to parse json')
return
}
for user in users {
println('$user.name: $user.age')
}
println('')
for i, user in users {
println('$i) $user.name')
if !user.can_register() {
println('Cannot register $user.name, they are too young')
}
}
// Let's encode users again just for fun
println('')
println(json.encode(users))
}

fn (u User) can_register() bool {
return u.age >= 16
}

fn (u mut User) register() {
u.is_registered = true
}

15 changes: 15 additions & 0 deletions samples/V/links_scraper.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import http

fn main() {
html := http.get_text('https://news.ycombinator.com')
mut pos := 0
for {
pos = html.index_after('https://', pos + 1)
if pos == -1 {
break
}
end := html.index_after('"', pos)
println(html.substr(pos, end))
}
}

12 changes: 12 additions & 0 deletions samples/V/log.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import log

fn main(){
mut l := log.Log{log.INFO, 'terminal'}
l.info('info')
l.warn('warn')
l.error('error')
l.debug('no debug')
l.set_level(log.DEBUG)
l.debug('debug')
l.fatal('fatal')
}
10 changes: 10 additions & 0 deletions samples/V/loop.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
fn main() {
numbers := [1, 2, 3, 4, 5]
for num in numbers {
println(num)
}
names := ['Sam', 'Peter']
for i, name in names {
println('$i) $name') // Output: 0) Sam, etc
}
}
132 changes: 132 additions & 0 deletions samples/V/nbody.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// Ported based on https://benchmarksgame-team.pages.debian.net/benchmarksgame/program/nbody-go-3.html
// Output:
// -0.169075164
// -0.169059907

import math

const (
SolarMass = 39.47841760435743197 //4.0 * math.Pi * math.Pi
DaysPerYear = 365.24
N = 5
)

struct Position {
pub mut:
x f64
y f64
z f64
}

struct Momentum {
pub mut:
x f64
y f64
z f64
m f64
}

struct System {
pub mut:
v []Momentum
s []Position
}

fn advance(sys System, dt f64) {
for i := 0; i < N - 1; i++ {
mut _vx := sys.v[i].x
mut _vy := sys.v[i].y
mut _vz := sys.v[i].z

for j := i + 1; j < N; j++ {
dx := sys.s[i].x - sys.s[j].x
dy := sys.s[i].y - sys.s[j].y
dz := sys.s[i].z - sys.s[j].z

dsquared := dx * dx + dy * dy + dz * dz
distance := math.sqrt(dsquared)
mag := (dt / (dsquared * distance))
mi := sys.v[i].m

_vx -= dx * sys.v[j].m * mag
_vy -= dy * sys.v[j].m * mag
_vz -= dz * sys.v[j].m * mag

sys.v[j].x += dx * mi * mag
sys.v[j].y += dy * mi * mag
sys.v[j].z += dz * mi * mag
}
sys.v[i].x = _vx
sys.v[i].y = _vy
sys.v[i].z = _vz
}

for i := 0; i < N; i++ {
sys.s[i].x += dt * sys.v[i].x
sys.s[i].y += dt * sys.v[i].y
sys.s[i].z += dt * sys.v[i].z
}
}

fn offsetmomentum(sys System) {
mut px := f64(0)
mut py := f64(0)
mut pz := f64(0)

for i := 0; i < N; i++ {
px += sys.v[i].x * sys.v[i].m
py += sys.v[i].y * sys.v[i].m
pz += sys.v[i].z * sys.v[i].m
}
sys.v[0].x = -px / SolarMass
sys.v[0].y = -py / SolarMass
sys.v[0].z = -pz / SolarMass
}

fn energy(sys System) f64 {
mut e := f64(0)
for i := 0; i < N; i++ {
e += 0.5 * sys.v[i].m * (sys.v[i].x * sys.v[i].x + sys.v[i].y * sys.v[i].y + sys.v[i].z * sys.v[i].z)
for j := i + 1; j < N; j++ {
dx := sys.s[i].x - sys.s[j].x
dy := sys.s[i].y - sys.s[j].y
dz := sys.s[i].z - sys.s[j].z
distance := math.sqrt(dx * dx + dy * dy + dz * dz)
e -= (sys.v[i].m * sys.v[j].m) / distance
}
}
return e
}

fn arr_momentum() []Momentum {
return [
Momentum {0.0, 0.0, 0.0, SolarMass},
Momentum {1.66007664274403694e-03 * DaysPerYear, 7.69901118419740425e-03 * DaysPerYear, -6.90460016972063023e-05 * DaysPerYear, 9.54791938424326609e-04 * SolarMass},
Momentum {-2.76742510726862411e-03 * DaysPerYear, 4.99852801234917238e-03 * DaysPerYear, 2.30417297573763929e-05 * DaysPerYear, 2.85885980666130812e-04 * SolarMass},
Momentum {2.96460137564761618e-03 * DaysPerYear, 2.37847173959480950e-03 * DaysPerYear, -2.96589568540237556e-05 * DaysPerYear, 4.36624404335156298e-05 * SolarMass},
Momentum {2.68067772490389322e-03 * DaysPerYear, 1.62824170038242295e-03 * DaysPerYear, -9.51592254519715870e-05 * DaysPerYear, 5.15138902046611451e-05 * SolarMass},
]
}

pub fn arr_position() []Position {
return [
Position {0.0, 0.0, 0.0},
Position {4.84143144246472090e+00, -1.16032004402742839e+00, -1.03622044471123109e-01},
Position {8.34336671824457987e+00, 4.12479856412430479e+00, -4.03523417114321381e-01},
Position {1.28943695621391310e+01, -1.51111514016986312e+01, -2.23307578892655734e-01},
Position {1.53796971148509165e+01, -2.59193146099879641e+01, 1.79258772950371181e-01},
]
}

fn main() {

sys := &System {arr_momentum(), arr_position()}
offsetmomentum(sys)

println('${energy(sys):.9f}') //-0.169075164
for i := 0; i < 50000000; i++ {
advance(sys, 0.01)
}
println('${energy(sys):.9f}') //-0.169059907

}
76 changes: 76 additions & 0 deletions samples/V/news_fetcher.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.

import http
import json
import sync

const (
NR_THREADS = 4
)

struct Story {
title string
url string
}

struct Fetcher {
mut:
mu sync.Mutex
ids []int
cursor int
wg &sync.WaitGroup
}

fn (f mut Fetcher) fetch() {
for {
f.mu.lock()
if f.cursor >= f.ids.len {
return
}
id := f.ids[f.cursor]
f.cursor++
cursor := f.cursor
f.mu.unlock()
resp := http.get('https://hacker-news.firebaseio.com/v0/item/${id}.json') or {
println('failed to fetch data from /v0/item/${id}.json')
exit(1)
}
story := json.decode(Story, resp.text) or {
println('failed to decode a story')
exit(1)
}
f.wg.done()
println('#$cursor) $story.title | $story.url')
}
}

// Fetches top HN stories in 4 coroutines
fn main() {
resp := http.get('https://hacker-news.firebaseio.com/v0/topstories.json') or {
println('failed to fetch data from /v0/topstories.json')
return
}
mut ids := json.decode([]int, resp.text) or {
println('failed to decode topstories.json')
return
}
if ids.len > 10 {
// ids = ids[:10]
mut tmp := [0 ; 10]
for i := 0 ; i < 10 ; i++ {
tmp[i] = ids[i]
}
ids = tmp
}

mut wg := &sync.WaitGroup{}
fetcher := &Fetcher{ids: ids, wg: wg} // wg sent via ptr
wg.add(ids.len)
for i := 0; i < NR_THREADS; i++ {
go fetcher.fetch()
}
wg.wait()
}

9 changes: 9 additions & 0 deletions samples/V/rune.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
fn main() {
// GRINNING FACE😀 => f0 09 98 80
grinning_face := rune(0xf09f9880)
println(grinning_face)

// COMMERCIAL AT@ => 0x40
commercial_at := rune(0x40000000)
println(commercial_at)
}
Loading