-
Lets
-
Enums
-
Functions
-
Classes
let CENTER = 15.0
This is the constant 'CENTER'.
let Parse = fn(lexer) {
fmt.Printf("This is the parser part!\n") //just a demo
fmt.Printf("The parser take a lexer as input, and generate an AST\n")
}
This is a demo parser
let SIZE = 10
This is the constant 'SIZE'.
enum Color{
GRAY = 5,
RED = 0,
BLUE = 1,
YELLOW = 2,
BLACK = 3,
WHITE = 4
}
Color enum is the color of the image. This is just for demo purpose.
enum LogOption{
LUTC = (1 << 5),
LstdFlags = ((1 << 4) | (1 << 5)),
Ldate = (1 << 0),
Ltime = (1 << 1),
Lmicroseconds = (1 << 2),
Llongfile = (1 << 3),
Lshortfile = (1 << 4)
}
LogOption enum is defined for Log options.
fn Add (x, y)
Add returns the addition of its two parameters
This is a example of calling Add
with two ints:
This is a example of calling Add
with two strings:
Name | Type | Description |
---|---|---|
x | int |
this is the first parameter |
y | int |
this is the second parameter |
-
int
the addition of it's two parameters if both parameters are ints. -
string
the string concatenation of it's two parameters if both parameters are strings
fn Add(x,y) {
return x + y
}
fn Div (x, y)
Div returns the division of its two parameters
fn Div(x,y) {
return x / y
}
fn Mul (x, y)
Mul returns the multiply of its two parameters
fn Mul(x,y) {
return x * y
}
fn Sub (x, y)
Sub returns the subtraction of its two parameters
Name | Type | Description |
---|---|---|
x | int |
this is the first parameter |
y | int |
this is the second parameter |
int
the subtraction of it's two parameters
fn Sub(x,y) {
return x - y
}
class @DepartmentValidator{ ... }
DepartmentValidator annotation class. It is used for checking a list of department.
class @DepartmentValidator {
property Department
}
class @MinMaxValidator{ ... }
MinMaxValidator annotation class It is used for checking min and max length The max length has a default value of 10
class @MinMaxValidator {
property MinLength
property MaxLength default 10
}
class @NoSpaceValidator{ ... }
NoSpaceValidator annotation class(it's a marker annotation). It is used for checking spaces.
class @NoSpaceValidator {}
class Person{ ... }
A Persion class which has three private variable:
- firstName
- lastName
- nickName
and two methods :'init' and 'Msg'.
let firstName;
this is the firstname
let lastName;
this is the last name
fn Msg (extra1, extra2, args)
Method 'Msg' demonstrate function default values and variable arguements
Name | Type | Description |
---|---|---|
extra1 | string |
this is extra1 |
extra2 | string |
this is extra2, it has default value "extra2" |
args | array |
this is args |
fn init (firstname, lastname, nickname)
This is the constructor of the Person class.
class Person {
//this is the firstname
let firstName;
//this is the last name
let lastName;
let nickName; // this is the nick name
//This is the constructor of the Person class.
fn init(firstname, lastname="huang", nickname="mike") {
this.firstName = firstname
this.lastName = lastname
this.nickName = nickname
}
/* Method 'Msg' demonstrate function default values and variable arguements
* @param {string} extra1 this is extra1
* @param {string} extra2 this is extra2, it has default value "extra2"
* @param {array} args this is args
*/
fn Msg(extra1, extra2="extra2", args...) {
printf("firstName=%s, lastName=%s, nickName=%s\n", firstName, lastName, nickName)
printf("extra1=%s, extra2=%s\n", extra1, extra2)
for i in args {
printf("i=%v\n", i)
}
}
}
class Request{ ... }
Request class represent a user made request.
class Request {
/* FirstName property, it's value's length should be between 1(minlenght) and 10(maxlength). */
@MinMaxValidator(MinLength=1)
property FirstName;
//LastName property, it's value should contain no space.
@NoSpaceValidator
property LastName;
//Dept property, it's value should be in a list ["Department of Education", "Department of Labors"].
@DepartmentValidator(Department=["Department of Education", "Department of Labors"])
property Dept;
}
class RequestHandler{ ... }
RequestHandler class is responsible for processing the annotation.
class RequestHandler {
//handle is the real logic of processing the annotations.
static fn handle(o) {
props = o.getProperties()
for p in props {
annos = p.getAnnotations()
for anno in annos {
if anno.instanceOf(MinMaxValidator) {
//p.value is the property real value.
if len(p.value) > anno.MaxLength || len(p.value) < anno.MinLength {
printf("Property '%s' is not valid!\n", p.name)
}
} elseif anno.instanceOf(NoSpaceValidator) {
for c in p.value {
if c == " " || c == "\t" {
printf("Property '%s' is not valid!\n", p.name)
break
}
}
} elseif anno.instanceOf(DepartmentValidator) {
found = false
for d in anno.Department {
if p.value == d {
found = true
}
}
if !found {
printf("Property '%s' is not valid!\n", p.name)
}
}
}
}
}
}
class RequestMain{ ... }
RequestMain class. It is the user/client code.
This is note one.
This is note two.
This is note three.
This is warning one.
This is warning two.
This is warning three.
class RequestMain {
static fn main() {
request = new Request()
request.FirstName = "Haifeng123456789"
request.LastName = "Huang "
request.Dept = "Department of Labors"
RequestHandler.handle(request)
}
}
Last updated 2018-03-05