Exemple #1
0
func (t *TimeMod) time_Date(args ...runtime.Val) runtime.Val {
	runtime.ExpectAtLeastNArgs(1, args)
	yr := int(args[0].Int())
	mth := 1
	if len(args) > 1 {
		mth = int(args[1].Int())
	}
	dy := 1
	if len(args) > 2 {
		dy = int(args[2].Int())
	}
	hr := 0
	if len(args) > 3 {
		hr = int(args[3].Int())
	}
	min := 0
	if len(args) > 4 {
		min = int(args[4].Int())
	}
	sec := 0
	if len(args) > 5 {
		sec = int(args[5].Int())
	}
	nsec := 0
	if len(args) > 6 {
		nsec = int(args[6].Int())
	}
	return t.newTime(time.Date(yr, time.Month(mth), dy, hr, min, sec, nsec, time.UTC))
}
Exemple #2
0
func (o *OsMod) os_Rename(args ...runtime.Val) runtime.Val {
	runtime.ExpectAtLeastNArgs(2, args)
	if e := os.Rename(args[0].String(), args[1].String()); e != nil {
		panic(e)
	}
	return runtime.Nil
}
Exemple #3
0
// Args:
// 0 - The string
// 1 - The regexp pattern
// 2 - (optional) a maximum number of matches to return
//
// Returns:
// An object holding all the matches, or nil if no match.
// Each match contains:
// n - The nth match group (when n=0, the full text of the match)
// Each match group contains:
// start - the index of the start of the match
// end - the end of the match
// text - the string of the match
func (s *StringsMod) strings_Matches(args ...runtime.Val) runtime.Val {
	runtime.ExpectAtLeastNArgs(2, args)
	src := args[0].String()
	rx := regexp.MustCompile(args[1].String())
	n := -1 // By default, return all matches
	if len(args) > 2 {
		n = int(args[2].Int())
	}
	strmtch := rx.FindAllStringSubmatch(src, n)
	if strmtch == nil {
		return runtime.Nil
	}
	ixmtch := rx.FindAllStringSubmatchIndex(src, n)
	ob := runtime.NewObject()
	for i, mtches := range strmtch {
		obch := runtime.NewObject()
		for j, mtch := range mtches {
			leaf := runtime.NewObject()
			leaf.Set(runtime.String("Text"), runtime.String(mtch))
			leaf.Set(runtime.String("Start"), runtime.Number(ixmtch[i][2*j]))
			leaf.Set(runtime.String("End"), runtime.Number(ixmtch[i][2*j+1]))
			obch.Set(runtime.Number(j), leaf)
		}
		ob.Set(runtime.Number(i), obch)
	}
	return ob
}
Exemple #4
0
func (m *MathMod) math_Min(args ...runtime.Val) runtime.Val {
	runtime.ExpectAtLeastNArgs(2, args)
	min := args[len(args)-1].Float()
	for i := len(args) - 2; i >= 0; i-- {
		min = math.Min(min, args[i].Float())
	}
	return runtime.Number(min)
}
Exemple #5
0
func (m *MathMod) math_Max(args ...runtime.Val) runtime.Val {
	runtime.ExpectAtLeastNArgs(2, args)
	max := args[len(args)-1].Float()
	for i := len(args) - 2; i >= 0; i-- {
		max = math.Max(max, args[i].Float())
	}
	return runtime.Number(max)
}
Exemple #6
0
func (o *OsMod) os_ReadFile(args ...runtime.Val) runtime.Val {
	runtime.ExpectAtLeastNArgs(1, args)
	b, e := ioutil.ReadFile(args[0].String())
	if e != nil {
		panic(e)
	}
	return runtime.String(b)
}
Exemple #7
0
func (fp *FilepathMod) filepath_Abs(args ...runtime.Val) runtime.Val {
	runtime.ExpectAtLeastNArgs(1, args)
	s, e := filepath.Abs(args[0].String())
	if e != nil {
		panic(e)
	}
	return runtime.String(s)
}
Exemple #8
0
// Args:
// 0 - The source string
// 1 - The 0-based index number
//
// Returns:
// The character at that position, as a string, or an empty string if
// the index is out of bounds.
func (s *StringsMod) strings_ByteAt(args ...runtime.Val) runtime.Val {
	runtime.ExpectAtLeastNArgs(2, args)
	src := args[0].String()
	at := int(args[1].Int())
	if at < 0 || at >= len(src) {
		return runtime.String("")
	}
	return runtime.String(src[at])
}
Exemple #9
0
// Args:
// 0 - the source string
// 1 [optional] - the cutset (all leading and trailing characters in this string will be
// removed). Defaults to whitespace (space, \n, \t, \v and \r).
// Returns:
// The trimmed string.
func (s *StringsMod) strings_Trim(args ...runtime.Val) runtime.Val {
	runtime.ExpectAtLeastNArgs(1, args)
	src := args[0].String()
	cut := " \n\t\v\r"
	if len(args) > 1 {
		cut = args[1].String()
	}
	return runtime.String(strings.Trim(src, cut))
}
Exemple #10
0
func (o *OsMod) os_Exec(args ...runtime.Val) runtime.Val {
	runtime.ExpectAtLeastNArgs(1, args)
	c := exec.Command(args[0].String(), toString(args[1:])...)
	b, e := c.CombinedOutput()
	if e != nil {
		panic(e)
	}
	return runtime.String(b)
}
Exemple #11
0
// Returns true if the string at arg0 starts with any of the following strings.
// Args:
// 0 - The source string
// 1..n - The prefixes to test
// Returns:
// true if the source string starts with any of the specified prefixes
func (s *StringsMod) strings_HasPrefix(args ...runtime.Val) runtime.Val {
	runtime.ExpectAtLeastNArgs(2, args)
	src := args[0].String()
	for _, v := range args[1:] {
		if strings.HasPrefix(src, v.String()) {
			return runtime.Bool(true)
		}
	}
	return runtime.Bool(false)
}
Exemple #12
0
// Args:
// 0 - The source string
// 1 - [Optional] the start index in the source string
// 2 (or 1) .. n - The substrings to search for in the source string.
// Returns:
// The last index of the first found substring in source, if any is found, or -1
func (s *StringsMod) strings_LastIndex(args ...runtime.Val) runtime.Val {
	runtime.ExpectAtLeastNArgs(2, args)
	src := args[0].String()
	start := 0
	find := 1
	switch v := args[1].(type) {
	case runtime.Number:
		runtime.ExpectAtLeastNArgs(3, args)
		start = int(v.Int())
		find = 2
	}
	src = src[start:]
	for _, v := range args[find:] {
		if ix := strings.LastIndex(src, v.String()); ix >= 0 {
			return runtime.Number(ix)
		}
	}
	return runtime.Number(-1)
}
Exemple #13
0
// Slice a string to get a substring. Basically the same as slicing in Go.
// Args:
// 0 - The source string
// 1 - The start index
// 2 [optional] - The high bound, such that the length of the resulting string is high-start
// Results:
// The sliced string.
func (s *StringsMod) strings_Slice(args ...runtime.Val) runtime.Val {
	runtime.ExpectAtLeastNArgs(2, args)
	src := args[0].String()
	start := args[1].Int()
	end := len(src)
	if len(args) > 2 {
		end = int(args[2].Int())
	}
	return runtime.String(src[start:end])
}
Exemple #14
0
// Converts strings to lowercase, concatenating all strings.
// Args:
// 0..n - The strings to convert to lower case and concatenate
// Returns:
// The lowercase string
func (s *StringsMod) strings_ToLower(args ...runtime.Val) runtime.Val {
	runtime.ExpectAtLeastNArgs(1, args)
	buf := bytes.NewBuffer(nil)
	for _, v := range args {
		_, err := buf.WriteString(strings.ToLower(v.String()))
		if err != nil {
			panic(err)
		}
	}
	return runtime.String(buf.String())
}
Exemple #15
0
func (o *OsMod) os_ReadDir(args ...runtime.Val) runtime.Val {
	runtime.ExpectAtLeastNArgs(1, args)
	fis, e := ioutil.ReadDir(args[0].String())
	if e != nil {
		panic(e)
	}
	ob := runtime.NewObject()
	for i, fi := range fis {
		ob.Set(runtime.Number(i), createFileInfo(fi))
	}
	return ob
}
Exemple #16
0
// Args:
// 0 - the source string
// 1 - the separator
// 2 [optional] - the maximum number of splits, defaults to all
// Returns:
// An array-like object with splits as values and indices as keys.
func (s *StringsMod) strings_Split(args ...runtime.Val) runtime.Val {
	runtime.ExpectAtLeastNArgs(2, args)
	src := args[0].String()
	sep := args[1].String()
	cnt := -1
	if len(args) > 2 {
		cnt = int(args[2].Int())
	}
	splits := strings.SplitN(src, sep, cnt)
	ob := runtime.NewObject()
	for i, v := range splits {
		ob.Set(runtime.Number(i), runtime.String(v))
	}
	return ob
}
Exemple #17
0
func (o *OsMod) os_WriteFile(args ...runtime.Val) runtime.Val {
	runtime.ExpectAtLeastNArgs(1, args)
	f, e := os.Create(args[0].String())
	if e != nil {
		panic(e)
	}
	defer f.Close()
	n := 0
	for _, v := range args[1:] {
		m, e := f.WriteString(v.String())
		if e != nil {
			panic(e)
		}
		n += m
	}
	return runtime.Number(n)
}
Exemple #18
0
// Args:
// 0 - The source string
// 1 - The old substring to replace
// 2 [optional] - the new substring to insert (none by default, delete only)
// 3 [optional] - the number of occurrences to replace. If 2 is a number, it is
// considered the value of 3 and 2 is empty.
// Returns:
// The string with n occurrences of old replaced by new.
func (s *StringsMod) strings_Replace(args ...runtime.Val) runtime.Val {
	runtime.ExpectAtLeastNArgs(2, args)
	src := args[0].String()
	old := args[1].String()
	nw := ""
	cnt := -1
	if len(args) > 2 {
		switch v := args[2].(type) {
		case runtime.Number:
			cnt = int(v.Int())
		default:
			// args[2] is the new string, args[3], if present, is the count
			nw = v.String()
			if len(args) > 3 {
				cnt = int(args[3].Int())
			}
		}
	}
	return runtime.String(strings.Replace(src, old, nw, cnt))
}
Exemple #19
0
// Args:
// 0 - The source object
// 1 - The separator, empty string by default
// Returns:
// The concatenated string of all the array-like indices of the source object.
func (s *StringsMod) strings_Join(args ...runtime.Val) runtime.Val {
	runtime.ExpectAtLeastNArgs(1, args)
	ob := args[0].(runtime.Object)
	sep := ""
	if len(args) > 1 {
		sep = args[1].String()
	}
	l := int(ob.Len().Int())
	buf := bytes.NewBuffer(nil)
	for i := 0; i < l; i++ {
		val := ob.Get(runtime.Number(i))
		if _, err := buf.WriteString(val.String()); err != nil {
			panic(err)
		}
		if i < l-1 {
			if _, err := buf.WriteString(sep); err != nil {
				panic(err)
			}
		}
	}
	return runtime.String(buf.String())
}
Exemple #20
0
func (o *OsMod) os_Open(args ...runtime.Val) runtime.Val {
	runtime.ExpectAtLeastNArgs(1, args)
	nm := args[0].String()
	flg := "r" // defaults to read-only
	if len(args) > 1 {
		// Second arg is the flag (same as C's fopen)
		// r  - open for reading
		// w  - open for writing (file need not exist)
		// a  - open for appending (file need not exist)
		// r+ - open for reading and writing, start at beginning
		// w+ - open for reading and writing (overwrite file)
		// a+ - open for reading and writing (append if file exists)
		flg = args[1].String()
	}
	var flgi int
	switch flg {
	case "r":
		flgi = os.O_RDONLY
	case "w":
		flgi = os.O_WRONLY | os.O_CREATE | os.O_TRUNC
	case "a":
		flgi = os.O_APPEND | os.O_CREATE
	case "r+":
		flgi = os.O_RDWR
	case "w+":
		flgi = os.O_RDWR | os.O_CREATE | os.O_TRUNC
	case "a+":
		flgi = os.O_RDWR | os.O_APPEND | os.O_CREATE
	default:
		panic("invalid file flag mode: " + flg)
	}
	f, e := os.OpenFile(nm, flgi, 0666)
	if e != nil {
		panic(e)
	}
	return o.newFile(f)
}
Exemple #21
0
func (m *MathMod) math_Atanh(args ...runtime.Val) runtime.Val {
	runtime.ExpectAtLeastNArgs(1, args)
	return runtime.Number(math.Atanh(args[0].Float()))
}
Exemple #22
0
func (m *MathMod) math_IsNaN(args ...runtime.Val) runtime.Val {
	runtime.ExpectAtLeastNArgs(1, args)
	return runtime.Bool(math.IsNaN(args[0].Float()))
}
Exemple #23
0
func (t *TimeMod) time_Sleep(args ...runtime.Val) runtime.Val {
	runtime.ExpectAtLeastNArgs(1, args)
	time.Sleep(time.Duration(args[0].Int()) * time.Millisecond)
	return runtime.Nil
}
Exemple #24
0
func (fp *FilepathMod) filepath_Join(args ...runtime.Val) runtime.Val {
	runtime.ExpectAtLeastNArgs(1, args)
	s := toString(args)
	return runtime.String(filepath.Join(s...))
}
Exemple #25
0
func (fp *FilepathMod) filepath_Dir(args ...runtime.Val) runtime.Val {
	runtime.ExpectAtLeastNArgs(1, args)
	return runtime.String(filepath.Dir(args[0].String()))
}
Exemple #26
0
func (m *MathMod) math_Pow(args ...runtime.Val) runtime.Val {
	runtime.ExpectAtLeastNArgs(2, args)
	return runtime.Number(math.Pow(args[0].Float(), args[1].Float()))
}
Exemple #27
0
func (s *StringsMod) strings_Repeat(args ...runtime.Val) runtime.Val {
	runtime.ExpectAtLeastNArgs(2, args)
	src := args[0].String()
	n := int(args[1].Int())
	return runtime.String(strings.Repeat(src, n))
}
Exemple #28
0
func (m *MathMod) math_IsInf(args ...runtime.Val) runtime.Val {
	runtime.ExpectAtLeastNArgs(2, args)
	return runtime.Bool(math.IsInf(args[0].Float(), int(args[1].Int())))
}
Exemple #29
0
func (fp *FilepathMod) filepath_IsAbs(args ...runtime.Val) runtime.Val {
	runtime.ExpectAtLeastNArgs(1, args)
	return runtime.Bool(filepath.IsAbs(args[0].String()))
}
Exemple #30
0
func (m *MathMod) math_RandSeed(args ...runtime.Val) runtime.Val {
	runtime.ExpectAtLeastNArgs(1, args)
	rand.Seed(args[0].Int())
	return runtime.Nil
}