vweb: first step to revive it

pull/2535/head
Alexander Medvednikov 2019-10-24 19:44:49 +03:00
parent 2032da7fe2
commit 892d1c6aab
7 changed files with 116 additions and 114 deletions

View File

@ -3,7 +3,7 @@ module main
import vweb import vweb
const ( const (
Port = 8082 port = 8082
) )
struct App { struct App {
@ -13,7 +13,9 @@ pub mut:
} }
fn main() { fn main() {
vweb.run<App>(Port) mut app := App{}
vweb.run(app, port)
//vweb.run<App>(Port)
} }
pub fn (app mut App) init() { pub fn (app mut App) init() {

View File

@ -275,8 +275,8 @@ fn (p mut Parser) fn_decl() {
// C function header def? (fn C.NSMakeRect(int,int,int,int)) // C function header def? (fn C.NSMakeRect(int,int,int,int))
is_c := f.name == 'C' && p.tok == .dot is_c := f.name == 'C' && p.tok == .dot
// Just fn signature? only builtin.v + default build mode // Just fn signature? only builtin.v + default build mode
if p.pref.build_mode == .build_module { if p.pref.is_verbose { // p.pref.build_mode == .build_module {
//println('\n\nfn_decl() name=$f.name receiver_typ=$receiver_typ nogen=$p.cgen.nogen') println('\n\nfn_decl() name=$f.name receiver_typ=$receiver_typ nogen=$p.cgen.nogen')
} }
if is_c { if is_c {
p.check(.dot) p.check(.dot)
@ -561,8 +561,10 @@ fn (p mut Parser) check_unused_variables() {
if !var.is_used && !p.pref.is_repl && !var.is_arg && !p.pref.translated { if !var.is_used && !p.pref.is_repl && !var.is_arg && !p.pref.translated {
p.production_error_with_token_index('`$var.name` declared and not used', var.token_idx ) p.production_error_with_token_index('`$var.name` declared and not used', var.token_idx )
} }
if !var.is_changed && var.is_mut && !p.pref.is_repl && !p.pref.translated { if !var.is_changed && var.is_mut && !p.pref.is_repl &&
p.error_with_token_index( '`$var.name` is declared as mutable, but it was never changed', var.token_idx ) !p.pref.translated && var.typ != 'T*'
{
p.error_with_token_index('`$var.name` is declared as mutable, but it was never changed', var.token_idx )
} }
} }
} }

View File

@ -472,7 +472,7 @@ fn (p mut Parser) import_statement() {
if p.tok != .name { if p.tok != .name {
p.error('bad import format') p.error('bad import format')
} }
if p.peek() == .number { // && p.scanner.text[p.scanner.pos + 1] == `.` { if p.peek() == .number {
p.error('bad import format. module/submodule names cannot begin with a number') p.error('bad import format. module/submodule names cannot begin with a number')
} }
import_tok_idx := p.token_idx-1 import_tok_idx := p.token_idx-1
@ -513,14 +513,6 @@ fn (p mut Parser) const_decl() {
if is_pub { if is_pub {
p.next() p.next()
} }
if p.tok == .key_import {
p.error_with_token_index(
'`import const` was removed from the language, ' +
'because predeclaring C constants is not needed anymore. ' +
'You can use them directly with C.CONST_NAME',
p.cur_tok_index()
)
}
p.inside_const = true p.inside_const = true
p.check(.key_const) p.check(.key_const)
p.fspace() p.fspace()
@ -659,13 +651,13 @@ fn (p mut Parser) interface_method(field_name, receiver string) &Fn {
fn key_to_type_cat(tok TokenKind) TypeCategory { fn key_to_type_cat(tok TokenKind) TypeCategory {
match tok { match tok {
.key_interface { return TypeCategory.interface_ } .key_interface { return .interface_ }
.key_struct { return TypeCategory.struct_ } .key_struct { return .struct_ }
.key_union { return TypeCategory.union_ } .key_union { return .union_ }
//TokenKind.key_ => return .interface_ //TokenKind.key_ => return .interface_
} }
verror('Unknown token: $tok') verror('Unknown token: $tok')
return TypeCategory.builtin return .builtin
} }
// check_name checks for a name token and returns its literal // check_name checks for a name token and returns its literal
@ -727,10 +719,10 @@ fn (p mut Parser) check(expected TokenKind) {
*/ */
p.next() p.next()
if p.scanner.line_comment != '' { //if p.scanner.line_comment != '' {
//p.fgenln('// ! "$p.scanner.line_comment"') //p.fgenln('// ! "$p.scanner.line_comment"')
//p.scanner.line_comment = '' //p.scanner.line_comment = ''
} //}
} }
@ -894,7 +886,7 @@ fn (p mut Parser) get_type() string {
p.error('unknown type `$typ`') p.error('unknown type `$typ`')
} }
} }
else if !t.is_public && t.mod != p.mod && t.name != '' { else if !t.is_public && t.mod != p.mod && t.name != '' && !p.first_pass() {
p.error('type `$t.name` is private') p.error('type `$t.name` is private')
} }
} }

View File

@ -81,6 +81,7 @@ fn (p mut Parser) struct_decl() {
typ.is_placeholder = false typ.is_placeholder = false
typ.cat = cat typ.cat = cat
typ.parent = objc_parent typ.parent = objc_parent
typ.is_public = is_pub
p.table.rewrite_type(typ) p.table.rewrite_type(typ)
} }
else { else {

View File

@ -11,7 +11,7 @@ const (
max_redirects = 4 max_redirects = 4
) )
struct Request { pub struct Request {
pub: pub:
headers map[string]string headers map[string]string
method string method string
@ -27,7 +27,7 @@ pub:
user_agent string user_agent string
} }
struct Response { pub struct Response {
pub: pub:
text string text string
headers map[string]string headers map[string]string

View File

@ -2,7 +2,7 @@ module net
import os import os
struct Socket { pub struct Socket {
pub: pub:
sockfd int sockfd int
family int family int

View File

@ -1,3 +1,7 @@
// 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.
module vweb module vweb
import ( import (
@ -27,15 +31,15 @@ const (
} }
) )
struct Context { pub struct Context {
static_files map[string]string static_files map[string]string
static_mime_types map[string]string static_mime_types map[string]string
pub: pub:
req http.Request req http.Request
conn net.Socket conn net.Socket
form map[string]string form map[string]string
// TODO Response // TODO Response
mut: mut:
headers string // response headers headers string // response headers
} }
@ -84,10 +88,11 @@ fn (ctx mut Context) get_header(key string) string {
return ctx.headers.find_between('\r\n$key: ', '\r\n') return ctx.headers.find_between('\r\n$key: ', '\r\n')
} }
pub fn run<T>(port int) { //pub fn run<T>(port int) {
pub fn run<T>(app T, port int) {
println('Running vweb app on http://localhost:$port ...') println('Running vweb app on http://localhost:$port ...')
l := net.listen(port) or { panic('failed to listen') } l := net.listen(port) or { panic('failed to listen') }
mut app := T{} //mut app := T{}
app.init() app.init()
for { for {
conn := l.accept() or { conn := l.accept() or {