summary refs log tree commit diff
path: root/modules/git.nix
blob: f7a26c4722cbe4e3f8066ae977a40866e64198ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
{ me, keys, pkgs, ... }:

{
  users.users.git = {
    isSystemUser = true;
    description = "Git user";
    shell = "${pkgs.git}/bin/git-shell";
    home = "/srv/git";
    group = "git";

    openssh.authorizedKeys.keys = keys.allUsers;
  };
  users.groups.git = {};
}
207' href='#n207'>207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
package compiler_test

import (
	"jinx/pkg/lang/compiler"
	"jinx/pkg/lang/parser"
	"jinx/pkg/lang/scanner"
	"jinx/pkg/lang/vm/text"
	"strings"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestSimpleAddExpr(t *testing.T) {
	src := `
	1 + 2
	`

	expected := `
	push_int 1
	push_int 2
	add
	drop 1
	halt
	`

	mustCompileTo(t, src, expected)
}

func TestOperationOrder(t *testing.T) {
	grouped := `
	(((1 + 2) - 3) + 4)
	`

	free := `
	1 + 2 - 3 + 4
	`

	expected := `
	push_int 1
	push_int 2
	add
	push_int 3
	sub
	push_int 4
	add
	drop 1
	halt
	`

	mustCompileTo(t, grouped, expected)
	mustCompileTo(t, free, expected)
}

func TestNestedExpr(t *testing.T) {
	// Yeah, we don't compile identifiers yet, so here we call and index directly on literals.
	src := `
	1 + 2 - 3 - (123("a", "b", "c") + 456[321])
	`

	expected := `
	push_int 1
	push_int 2
	add
	
	push_int 3
	sub

	push_int 123
	push_string "a"
	push_string "b"
	push_string "c"
	call 3

	push_int 456
	push_int 321
	index

	add
	sub

	drop 1

	halt
	`

	mustCompileTo(t, src, expected)
}

func TestVars(t *testing.T) {
	src := `
	var x = 10
	var y = 25
	x = x + 7

	var res = x + y
	`

	expected := `
	push_int 10

	push_int 25

	get_local 0
	push_int 7
	add
	set_local 0

	get_local 0
	get_local 1
	add

	halt
	`

	mustCompileTo(t, src, expected)
}

func TestArrayLit(t *testing.T) {
	src := `var x = [1, 2, 3]`

	expected := `
	push_array

	get_local 0
	get_member "push"
	push_int 1
	call 1
	drop 1

	get_local 0
	get_member "push"
	push_int 2
	call 1
	drop 1

	get_local 0
	get_member "push"
	push_int 3
	call 1
	drop 1

	halt
	`

	mustCompileTo(t, src, expected)
}

func TestIf(t *testing.T) {
	src := `
	if 1 <= 5 {
		"hello " + "world"
	}
	`

	expected := `
	push_int 1
	push_int 5
	lte

	jf @end

	push_string "hello "
	push_string "world"
	add

	drop 1

	@end:
	halt
	`

	mustCompileTo(t, src, expected)
}

func TestIfElifElse(t *testing.T) {
	src := `
		if false {
			1
		} elif true {
			2
		} else {
			3
		}
	`

	expected := `
	push_false
	jf @elif

	push_int 1
	drop 1
	jmp @end

	@elif:
	push_true
	jf @else
	push_int 2
	drop 1
	jmp @end

	@else:
	push_int 3
	drop 1

	@end:
	halt
	`

	mustCompileTo(t, src, expected)
}

func TestIfNoElse(t *testing.T) {
	src := `
		if false {
			1
		} elif true {
			2
		}
	`

	expected := `
	push_false
	jf @elif

	push_int 1
	drop 1
	jmp @end

	@elif:
	push_true
	jf @end
	push_int 2
	drop 1

	@end:
	halt
	`

	mustCompileTo(t, src, expected)
}

func TestNestedIfs(t *testing.T) {
	src := `
	if true {
		if false {
			1
		} else {
			2
		}
	}
	`

	expected := `
	push_true
	jf @end

	push_false
	jf @else

	push_int 1
	drop 1
	jmp @end

	@else:
	push_int 2
	drop 1
	
	@end:
	halt
	`

	mustCompileTo(t, src, expected)
}

func TestForCond(t *testing.T) {
	src := `
	for true {
		for false {
			1
		}
	}
	`

	expected := `
	@start:
	push_true
	jf @end

	@inner_start:
	push_false
	jf @inner_end

	push_int 1
	drop 1
	jmp @inner_start

	@inner_end:
	jmp @start

	@end
	halt
	`

	mustCompileTo(t, src, expected)
}

func TestForCondBreakContinue(t *testing.T) {
	src := `
	var sum = 0
	var x = 0
	for {
		if x % 2 == 0 {
			continue
		}

		sum = sum + x

		if x == 100 {
			break
		}
	}
	`

	expected := `
	push_int 0
	push_int 0

	@continue:
	get_local 1
	push_int 2
	mod
	push_int 0
	eq
	jf @sum
	jmp @continue

	@sum:
	get_local 0
	get_local 1
	add
	set_local 0

	get_local 1
	push_int 100
	eq
	jf @repeat
	jmp @break

	@repeat:
	jmp @continue

	@break:
	halt
	`

	mustCompileTo(t, src, expected)
}

func TestForIn(t *testing.T) {
	src := `
	for x in [1, 2, 3, "oops"] {
		if x == "oops" {
			break
		}

		"say"(x)
	}
	`

	expected := `
	push_array

	get_local 0
	get_member "push"
	push_int 1
	call 1
	drop 1

	get_local 0
	get_member "push"
	push_int 2
	call 1
	drop 1

	get_local 0
	get_member "push"
	push_int 3
	call 1
	drop 1

	get_local 0
	get_member "push"
	push_string "oops"
	call 1
	drop 1

	push_int 0
	push_null

	@check:
	get_local 1
	get_local 0
	get_member "length"
	call 0
	lt

	jf @end

	get_local 0
	get_local 1
	index
	set_local 2

	get_local 1
	push_int 1
	add
	set_local 1

	get_local 2
	push_string "oops"
	eq
	jf @say

	jmp @end

	@say:
	push_string "say"
	get_local 2
	call 1
	drop 1

	jmp @check
	@end:
	drop 3
	halt
	`

	mustCompileTo(t, src, expected)
}

func TestSimpleFunction(t *testing.T) {
	src := `
	fn the_meaning_of_life() {
		return 42
	}

	var result = the_meaning_of_life()
	`

	expected := `
	push_function @the_meaning_of_life

	get_local 0
	call 0
	halt

	@the_meaning_of_life:
	push_int 42
	ret
	`

	mustCompileTo(t, src, expected)
}

func TestFunctionArgs(t *testing.T) {
	src := `
	fn add(a, b) {
		return a + b
	}
	
	add(4, 5)
	`

	expected := `
	push_function @add
	set_arg_count 2

	get_local 0
	push_int 4
	push_int 5
	call 2
	drop 1
	halt

	@add:
	get_local 0
	get_local 1
	add
	ret
	`

	mustCompileTo(t, src, expected)
}

func TestClosureEnv(t *testing.T) {
	src := `
	fn create() {
		var x = 0
		fn closure() {
			x = x + 1
			return x
		}

		return closure
	}
	`

	expected := `
	push_function @create
	halt

	@closure:
	get_env 0
	push_int 1
	add
	set_env 0
	get_env 0
	ret

	@create:
	push_int 0
	push_function @closure
	add_to_env 0

	get_local 1
	ret
	`

	mustCompileTo(t, src, expected)
}

func TestType(t *testing.T) {
	src := `
	type Cat {
		(name, age) {
			this.name = name
			this.age = age
		}

		fn meow(this) {
			return this.name + " says Meow!"
		}
	}

	var kitty = Cat("Kitty", 3)
	kitty.meow()
	`

	expected := `
	push_type "Cat"

	get_local 0
	get_member "$add_method"

	push_string "$init"
	push_function @Cat:$init
	set_arg_count 2

	call 2
	drop 1

	get_local 0
	get_member "$add_method"

	push_string "meow"
	push_function @Cat:meow

	call 2
	drop 1

	get_local 0
	push_string "Kitty"
	push_int 3
	call 2

	get_local 1
	get_member "meow"
	call 0
	drop 1

	halt

	@Cat:$init:
		push_object
		get_env 0
		anchor_type

		get_local 2
		get_local 0
		set_member "name"

		get_local 2
		get_local 1
		set_member "age"

		get_local 2
		ret
	@Cat:meow:
		get_env 0
		get_member "name"

		push_string " says Meow!"

		add
		ret
	`

	mustCompileTo(t, src, expected)
}

func TestGlobals(t *testing.T) {
	src := `
	use mul from hello by mel
	use pi from math

	var result = mul(pi, 2)
	`

	expected := `
	get_global "mel:hello:mul"
	get_global ":math:pi"
	push_int 2
	call 2
	halt
	`

	mustCompileTo(t, src, expected)
}

func TestLvalueAssign(t *testing.T) {
	src := `
	var v = 1
	var obj = null
	var arr = [1]

	v = 2
	obj.x = 2
	arr[0] = 2
	`

	expected := `
	push_int 1

	push_null

	push_array
	get_local 2
	get_member "push"
	push_int 1
	call 1
	drop 1

	push_int 2
	set_local 0

	get_local 1
	push_int 2
	set_member "x"

	get_local 2
	push_int 0
	push_int 2
	set_at_index
	halt
	`

	mustCompileTo(t, src, expected)
}

func mustCompileTo(t *testing.T, src, expected string) {
	scanner := scanner.New(strings.NewReader(src))
	tokens, err := scanner.Scan()
	require.NoError(t, err)

	parser := parser.New(tokens)
	program, err := parser.Parse()
	require.NoError(t, err)

	langCompiler := compiler.New("test_program", "test", program)
	testResult, err := langCompiler.Compile()
	require.NoError(t, err)

	textCompiler := text.NewCompiler(strings.NewReader(expected))
	expectedResult, err := textCompiler.Compile()
	require.NoError(t, err)

	if !assert.Equal(t, expectedResult.Code(), testResult.Code().Code()) {
		d1 := text.NewDecompiler(expectedResult)
		d2 := text.NewDecompiler(*testResult.Code())
		require.Equal(t, d1.Decompile(), d2.Decompile())
	}
}